ios – UITableViewCell字幕.没有让它工作

前端之家收集整理的这篇文章主要介绍了ios – UITableViewCell字幕.没有让它工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用 Xcode 4.6,我试图显示一个典型的UITableView及其带字幕的单元格.

如果我没错,代码是这样的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

Test *myTest = [self listTests][indexPath.row];

cell.textLabel.text = [myTest name];

UIFont *cellFont = [UIFont systemFontOfSize:16.0];
cell.textLabel.font = cellFont;

UIFont *detailFont = [UIFont systemFontOfSize:12.0];
NSMutableString *detailText = [NSMutableString stringWithFormat:@"%d",[myTest numQuestions]];
[detailText appendString:@" preguntas."];

cell.detailTextLabel.text = detailText;
cell.detailTextLabel.font = detailFont;

return cell;

}

出于某种原因,它永远不会通过这行代码

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

因此,永远不会使用UITableViewCellStyleSubtitle初始化单元格.

在进行[tableView dequeueReusableCellWithIdentifier:CellIdentifier]时,它总是以一种有效的单元格开始;

我能做错什么?

真的很难受.我总是使用这个代码,它通常有效.我还能在其他地方做错什么?

解决方法

使用故事板原型定义单元格时会发生这种情况.在这种情况下,可重用的单元格是使用initWithCoder:方法预先创建的,因此如果(cell == nil)永远不会被命中.有关更多信息,请参见 this question.

由于您似乎希望使用具有标准样式的单元格,因此将表格更改为不使用故事板原型或将原型设置为“Subtitle”应该可以解决此问题.

猜你在找的Xcode相关文章