Đ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
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. –
Vâng, đó là vấn đề của tôi. Bây giờ nó hoạt động tốt. Cảm ơn bạn. – pdenlinger