我试图让 UITableView 显示我创建的 headerView.xib,但是在构建它之后,什么都没有显示。想让 UITableView 可编辑,并在 ItemsViewController.m 文件中添加了三个方法,但没有显示。
怎么了?提前致谢。
以下是相关文件:
ItemsViewController.h
#import <Foundation/Foundation.h>
@interface ItemsViewController : UITableViewController
{
IBOutlet UIView *headerView;
}
-(UIView *)headerView;
-(IBAction)addNewItem:(id)sender;
-(IBAction)toggleEditingMode:(id)sender;
@end
项目视图控制器.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