从NIB加载自定义UITableViewCell问题

时间:2011-04-18 12:25:53

标签: iphone uitableview nib

更新:我现在已经解决了这个问题 - 请参阅我自己的评论。令人惊讶的是,当你再次通过它时,写出问题的频率会导致分辨率!


我必须阅读关于SO的所有帖子以及Apple上的示例,但我不能让这个在我的案例中工作,我想知道我做错了什么?

我在IB中使用XCode 4创建了一个自定义单元格。文件的所有者设置为表格的自定义视图控制器的类,我有一个用于UITableView单元格的IBOutlet,它可以从我所知道的内容中连接起来。我通常通过代码完成所有表格单元格并使用快速滚动方法,但对于这个项目,我真的需要使用笔尖来加载单元格内容,我有一些无法弄清楚的悲伤。

所以在我的界面文件中,我有;

@interface myCustomTableViewController <various delegates> {

   UITableViewCell *customNibCell;
   ...     
}

@property (nonatomic,assign) IBOutlet UITableViewCell *customNibCell;

在customTableViewController中,我在cellForRowAtIndexPath中执行此操作;

   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@:"customDirCell"];

    if (cell == nil) {

        [[NSBundle mainBundle] loadNibNamed:@"DirectoryCellWithMenu" owner:self options: nil];

        cell = customNibCell;
        self.customNibCell = nil;

     }

     [self configureCell:cell atRow:indexPath.row];

我的configureCell方法执行此操作以进入单元格并提取我想要自定义的内容。

UIImageView *fileImageView = (UIImageView *)[cell viewWithTag:3];
UILabel *headingTextLabel = (UILabel *)[cell viewWithTag:4];
UILabel *modifiedTextLabel = (UILabel *)[cell viewWithTag:5];
UILabel *sizeTextLabel = (UILabel *)[cell viewWithTag:6];

问题

单元格确实加载到表格中,因为我可以看到正确的布局。此外,我在各种按钮上定义的自定义操作都可以正常启动,因此看起来该单元格已正确连接。

但是没有一个定制工作 - 我似乎无法更改任何标签的文本,例如调查,每个[cell viewWithTag:nn]的结果都是nil 明确标出问题,但我看不出原因是什么。

  1. 的标识符 UITableViewCell设置为 “customDirCell”
  2. 标签的对象标识 确实是3,4,5,6根据 IB
  3. xib正好包含1个对象, UITableViewCell
  4. 我无法看到nib文件创建或代码有什么问题,但我显然错过了一些东西!

    任何善良的灵魂都能揭示我应该在哪里寻找它吗?

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@:"customDirCell"];

    if (cell == nil) {

       NSArray *nib =  [[NSBundle mainBundle] loadNibNamed:@"DirectoryCellWithMenu" owner:self options: nil];

        cell =[nib objectAtIndex:0];
        self.customNibCell = nil;

     }

答案 1 :(得分:0)

根据我的评论,我已经解决了这个问题。

相关问题