我正在尝试为我的表格 View 单元格添加副标题,但它们没有显示。 哪里错了?
是行
[[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;
}
最佳答案
这段代码:
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
永远不会执行,因为 dequeueReusableCellWithIdentifier: forIndexPath:
保证分配一个新单元格。
不幸的是,registerClass:forCellReuseIdentifier:
不允许您指定 UITableViewCellStyle
。
将 dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath
更改为简单的 dequeueReusableCellWithIdentifier:CellIdentifier
。此方法不保证将返回一个单元格。*如果不是,您的代码将创建一个具有您想要的样式的新单元格。
* -(如 rdelmar 指出的那样,如果您使用的是 Storyboard,它会这样做,但这里不是这种情况。)
关于ios - IOS7 上的 Tableview 字幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19694493/