我试图让一个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;
@endItemsViewController.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发布于 2012-04-30 05:48:23
以下一项或两项设置不正确。首先,HeaderView.xib中的文件所有者对象需要将其类设置为ItemsViewController。之后,需要将headerView插座从文件的所有者连接到xib中的顶级视图。
发布于 2012-07-31 17:45:11
我也遇到了同样的问题,并通过将comment //Load HeaderView.xib下面的代码行改为
// Load HeaderView.xib
headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] lastObject];nib文件只有一个视图,所以我给它分配了最后一个(唯一的)视图。
希望这能有所帮助。
发布于 2012-04-30 03:28:44
headerView方法内部的loadNibNamed方法返回nib文件内容的数组,这一点似乎很重要,但您并没有对这些内容做任何处理。也许您应该找到未存档对象内部的视图,并从nib文件内部将headerView设置为相应的视图。
https://stackoverflow.com/questions/10375043
复制相似问题