Rom*_*nge 6 iphone objective-c uitableview ios
目前我有自定义UITableViewCells的新闻源,例如:
NewsCell.m
NewsCell.xib
NewsCell_Friends.m
NewsCell_Friends.xib
NewsCell_CheckinPhoto.m
NewsCell_CheckinPhoto.xib
Run Code Online (Sandbox Code Playgroud)
[...]
NewsCell_Friends和NewsCell_Checkin继承了NewsCell(所有子类共享的方法和元素,如titleLabel,dateLabel)
目前,我从未使用过NewsCell类本身,只使用子类,因为每种新闻都有一个非常独特的布局(Xib).
现在让我们说,我希望在我的新闻源的UI方面有一个简单的实现,其中所有单元格具有相同的外观,相同的高度,相同的内容:titleLabel和dateLabel.(例如实际上是通知提要).
我想要做的是重新使用NewsCell.xib作为每种类型新闻的独立消息,例如下面:
NewsCell_CheckinPhoto *cell = [tableView dequeueReusableCellWithIdentifier:@"CheckinPhoto"];
if (!cell){
UIViewController *c = [[UIViewController alloc] initWithNibName:@"NewsCell" bundle:nil];
cell = (NewsCell_CheckinPhoto *)c.view;
cell.parent = self;
}
[cell configureCell:indexPath withNews:news];
return cell;
Run Code Online (Sandbox Code Playgroud)
在cellForRowAtIndexPath委托方法中.
请注意,我在此控制器中使用的是NewsCell xib而不是NewsCell_CheckinPhoto xib.
但它不起作用:nib文件加载良好,NewsCell.xib的内容很好地显示在单元格中,但标签的配置(例如,titleLabel.text = @"Robert拍了一张照片")在NewsCell_CheckinPhoto中阶级既不工作也不称呼.
它仅在我在NewsCell.xib文件中指定类类型*NewsCell_CheckinPhoto*时才有效,但这不允许我重复使用NewsCell.xib来呈现具有样本和唯一表示的NewsCell的每个子类.
当我理解您正确时,您想从 XIB 加载单元格。
你不应该这样做
UIViewController *c = [[UIViewController alloc] initWithNibName:@"NewsCell" bundle:nil];
但只将单元格放入 XIB 中,然后加载它
UITableViewCell *cell = [[NSBundle mainBundle] loadNibNamed:@"NewsCell"][0];
或类似的(请检查文档)。
另请记住,当您从 XIB 加载诸如表视图单元之类的内容时,它是用initWithCoder:、 notinit或初始化的initWithFrame:。这可能就是您的网点未初始化的原因。