ios - IOS7 上的 Tableview 字幕

标签 ios objective-c uitableview ios7

我正在尝试为我的表格 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/

相关文章:

ios - 为什么在cellForItemAt中设置单元格属性不好?

iphone - 在具有多个部分的 UITableView 中进行 Segue,每个部分包含从单个数组中过滤的对象

php - 调用 PHP Web 服务时,来自 iOS 的 $_POST 数据丢失

objective-c - UITableView 中心单元格详细信息

iphone - 将 CGSize 转换为 CGFloat 并使用它来调整 RoundRectButton 的大小

ios - 重复使用的单元格具有来自前一个单元格的 UIView

ios - 如何知道选择了哪个部分? swift

objective-c - iOS 双方法参数失败

iOS - 图像大小与 ImageView 大小

ios - 如何在 UITableViewCell 之间添加间距