2012-04-29 28 views
5

Đang cố gắng để có được một UITableView để hiển thị một headerView.xib mà tôi đã tạo ra, nhưng sau khi xây dựng nó, không có gì cho thấy. Bạn muốn làm cho UITableView có thể chỉnh sửa và đã thêm ba phương thức vào tệp ItemsViewController.m, nhưng không có gì hiển thị.UITableView sẽ không hiển thị headerView.xib

Có vấn đề gì? Cảm ơn trước.

Dưới đây là các tập tin liên quan:

ItemsViewController.h

#import <Foundation/Foundation.h> 

@interface ItemsViewController : UITableViewController 
{ 
    IBOutlet UIView *headerView; 
} 

-(UIView *)headerView; 
-(IBAction)addNewItem:(id)sender; 
-(IBAction)toggleEditingMode:(id)sender; 

@end 

ItemsViewController.m

#import "ItemsViewController.h" 
#import "BNRItemStore.h" 
#import "BNRItem.h" 

@implementation ItemsViewController // Incomplete implementation 

-(id)init 
{ 
    // Call the superclass's designated initializer 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 
    for(int i = 0; i < 5; i++) { 
     [[BNRItemStore sharedStore]createItem]; 
    } 

    } 
    return self; 
} 

-(id)initWithStyle:(UITableViewStyle)style 
{ 
    return [self init]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[BNRItemStore sharedStore]allItems]count]; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    // Check for a reusable cell first, use that if it exists 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    // If there is no reusable cell of this type, create a new one 
    if (!cell) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
    } 

    // Set the text on the cell with the description of the item 
    // that is at the nth index of items, where n = row this cell 
    // will appear in on the tableview 
    BNRItem *p = [[[BNRItemStore sharedStore]allItems]objectAtIndex:[indexPath row]]; 

    [[cell textLabel]setText:[p description]]; 

    return cell; 
} 

-(UIView *)headerView 
{ 
    // If we haven't loaded the headerView yet 
    if (!headerView) { 
    //Load HeaderView.xib 
    [[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil]; 
    } 
    return headerView; 
} 

-(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec 
{ 
    return [self headerView]; 
} 

-(CGFloat)tableView:(UITableView *)tv heightForHeaderInSection:(NSInteger)sec 
{ 
    // The height of the header view should be determined from the height of the 
    // view in the XIB file 
    return [[self headerView]bounds].size.height; 
} 



@end 
+0

Trong xib của tiêu đề, có phải là kết nối được thực hiện giữa chế độ xem tiêu đề và Chủ sở hữu tệp không? Chủ sở hữu tệp cũng nên đặt lớp của nó trong IB thành ItemsViewController. –

+0

Vâng, đó là vấn đề của tôi. Bây giờ nó hoạt động tốt. Cảm ơn bạn. – pdenlinger

Trả lời

3

Một hoặc cả hai điều sau đây không được thiết lập chính xác. Đầu tiên, đối tượng Chủ sở hữu tệp trong HeaderView.xib cần phải đặt lớp của nó thành ItemsViewController. Sau đó, cửa hàng headerView cần được kết nối từ Chủ sở hữu tệp đến chế độ xem cấp cao nhất trong xib.

0

Dường như có ý nghĩa rằng loadNibNamed phương pháp bên trong của phương pháp headerView của bạn trả về một mảng các nội dung của tệp nib, nhưng bạn đã không làm gì với những nội dung đó. Có thể bạn sẽ tìm thấy khung nhìn bên trong các đối tượng không lưu trữ và đặt headerView của bạn thành chế độ xem tương ứng từ bên trong tệp nib.

0

Bạn có bỏ lỡ triển khai phương pháp này không? Tôi không chắc chắn những lớp này đã được đặt giá trị mặc định của phần = 1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
3

tôi đã cùng một vấn đề và giải quyết nó bằng cách thay đổi dòng mã dưới comment // Nạp HeaderView.xib để

// Load HeaderView.xib 
    headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] lastObject]; 

các tập tin nib chỉ có một cái nhìn vì vậy tôi gán cho nó cuối cùng (chỉ) lượt xem.

hy vọng điều này sẽ hữu ích.

Các vấn đề liên quan