IOS7上的Tableview字幕

Spr*_*iel 8 objective-c uitableview ios ios7

我正在尝试为我的tableview单元格添加一个副标题,但它们不会显示.哪里出错了?

该行是 [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle] 最新的,还是iOS 7?

最好的祝福

坦率


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

    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = @"TestA";
    cell.detailTextLabel.text = @"TestB";

    return cell;
}
Run Code Online (Sandbox Code Playgroud)

Aar*_*ger 5

这段代码:

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

将永远不会执行,因为dequeueReusableCellWithIdentifier: forIndexPath:保证分配新的单元格.

不幸的是,registerClass:forCellReuseIdentifier:不允许你指定一个UITableViewCellStyle.

更改dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath简单dequeueReusableCellWithIdentifier:CellIdentifier.此方法不保证将返回单元格.*如果不是,则代码将创建具有所需样式的新单元格.


* - (如果你正在使用故事板,就像rdelmar指出的那样,但在这里并非如此.)