2011-04-18 110 views
0

更新:我现在已经解决了这个问题 - 请参阅我自己的意见。令人惊讶的是,多次写出问题可能会在您再次浏览时导致解决问题!从NIB加载自定义UITableViewCell问题


我必须阅读每个线程的SO和苹果的例子,但我不能得到这个在我的情况下工作,我不知道我做错了吗?

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

所以在我的接口文件中,

@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]; 

的问题

细胞都加载到表中尽可能多的好,我可以看到正确的布局。此外,我在各种按钮上定义的自定义操作都会触发OK,因此看起来单元格已正确连接。

但没有定制的作品 - 我似乎无法改变任何标签,例如和调查的文本,每次的结果[细胞viewWithTag:NN]是零其中明确路标的问题,但我看不出原因是什么。

  1. 用于 的UITableViewCell标识符被设定为 “customDirCell”
  2. 的对象标识为标签 等确实-3,4,5,6-根据 IB
  3. 的XIB正好包含1对象, UITableViewCell

我看不出任何错误的创建nib文件或代码,但我显然缺少一些东西!

任何一种灵魂都可以在我应该寻找的地方发光吗?

+0

ARGGGH。好的,所以我发现了这个问题。我混淆了Object属性(由IB设置)和Tag属性 - 这是我手动设置的。希望这可以帮助其他可能陷入我陷阱的人! – Roger 2011-04-18 12:30:46

回答

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; 

    }