UITableViewCell字幕.没有让它工作

ElP*_*ter 4 xcode objective-c uitableview ios

使用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;
Run Code Online (Sandbox Code Playgroud)

}

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

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
Run Code Online (Sandbox Code Playgroud)

因此,细胞永远不会被初始化UITableViewCellStyleSubtitle.

在做的时候,它总是从最初开始就是一个有效的细胞 [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

我能做错什么?

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

das*_*ght 9

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

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