首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITableView不显示headerView.xib

UITableView不显示headerView.xib
EN

Stack Overflow用户
提问于 2012-04-30 03:03:15
回答 4查看 2.4K关注 0票数 5

我试图让一个UITableView来显示我创建的headerView.xib,但在构建它之后,什么也没有显示。我想使UITableView可编辑,并向ItemsViewController.m文件添加了三个方法,但什么也没有显示。

怎么了?提前谢谢。

以下是相关文件:

ItemsViewController.h

代码语言:javascript
运行
复制
#import <Foundation/Foundation.h>

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

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

@end

ItemsViewController.m

代码语言:javascript
运行
复制
#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
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-04-30 05:48:23

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

票数 3
EN

Stack Overflow用户

发布于 2012-07-31 17:45:11

我也遇到了同样的问题,并通过将comment //Load HeaderView.xib下面的代码行改为

代码语言:javascript
运行
复制
// Load HeaderView.xib
    headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] lastObject];

nib文件只有一个视图,所以我给它分配了最后一个(唯一的)视图。

希望这能有所帮助。

票数 3
EN

Stack Overflow用户

发布于 2012-04-30 03:28:44

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10375043

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档