【发布时间】:2012-03-09 04:50:03
【问题描述】:
我正在转换到 iOS 5 和情节提要。当我有一个具有默认单元格样式的表格视图时,一切正常。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifierFromStoryboard"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifierFromStoryboard"];
}
return cell;
}
我已经看到了删除“if (cell == nil)”块的示例。但是,如果我把它拿出来,我的应用程序会崩溃并显示以下消息:“UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:”。这不是问题,因为它的工作原理如上所示。
我的问题是我想为单元格使用自定义样式,因此无法使用 initWithStyle。如何初始化我在情节提要上设计的自定义单元格?
旧的 pre-5 应用有一个 nib 和 class 使用类似的东西,但现在我正在使用情节提要。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
MyCustomTableCell *cell = (MyCustomTableCell *) [tableView dequeueReusableCellWithIdentifier:@"MyCustomIdentifier"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomTableCell" owner:self options:nil];
cell = (MyCustomTableCell *) [nib objectAtIndex:0];
}
return cell;
}
【问题讨论】:
-
如果您仍然尝试将此 Nib 用作自定义 TableViewCell 怎么办?
标签: ios ios5 uitableview storyboard