UITableView不会显示headerView.xib

时间:2012-04-29 19:03:15

标签: objective-c uitableview

我试图让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

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

4 个答案:

答案 0 :(得分:3)

未正确设置以下一项或两项内容。首先,HeaderView.xib中的File的Owner对象需要将其类设置为ItemsViewController。之后,headerView出口需要从文件所有者连接到xib中的顶级视图。

答案 1 :(得分:3)

我遇到了同样的问题并通过更改注释// Load HeaderView.xib

下的代码行来解决了这个问题
// Load HeaderView.xib
    headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] lastObject];

nib文件只有一个视图,因此我将它分配给最后一个(唯一)视图。

希望这会有所帮助。

答案 2 :(得分:0)

loadNibNamed方法内部的headerView方法返回nib文件内容的数组似乎很重要,但你对这些内容没有做任何事情。也许您应该在未归档对象中找到视图,并将headerView设置为nib文件内部的相应视图。

答案 3 :(得分:0)

您是否错过了实施这些方法?我不确定这些类是否已设置默认部分值= 1。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
相关问题