为什么我的UITableViewCell没有以任何风格显示detailTextLabel? [英] Why is my UITableViewCell not showing detailTextLabel in ANY style?

查看:123
本文介绍了为什么我的UITableViewCell没有以任何风格显示detailTextLabel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个让我把头发拉出来。我只是想用UITableViewCellStyleSubtitle样式创建一个带有文本和细节的表。但细节没有出现。通常这是因为有人忘记用正确的样式初始化单元格,但这不是在这种情况下发生的事情。我已经尝试了所有四种单元格样式,并且细节没有出现在任何单元格中,尽管当我使用UITableViewCellStyleValue2时文本标签确实向右移动。

This one has me pulling my hair out. I'm just trying to create a table with the UITableViewCellStyleSubtitle style, with text and detail. But the detail isn't showing up. Usually this is because someone has forgotten to initialize the cells with the correct style, but that's not what's going on in this case. I've tried all four cell styles, and the detail doesn't show up in any of them, though the text label does move over to the right when I use UITableViewCellStyleValue2.

我之前已多次成功创建过表格。在这种情况下,我能想到的唯一重要区别是我没有使用UITableViewController。相反,我像任何其他视图一样嵌入UITableView,并连接我自己的数据源和委托。

I've successfully created tables many times before. The only significant difference I can think of in this case is that I'm not using a UITableViewController. Instead, I'm embedding the UITableView like any other view, and hooking up my own data source and delegate.

我已经将关键方法归结为:

I've boiled down the key method to this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // Get the UITableViewCell (out of the recycling pool, or de novo)
    NSString *reuseID = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (not cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease];
    }

    cell.textLabel.text = @"Foo";
    cell.detailTextLabel.text = @"Bar";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22);        // required
    cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12];   // also required
    //[cell setNeedsLayout];  (makes no difference)
    return cell;
}

只有在我同时包含必需的内容时才能看到详细信息上面的行(并使用除Default之外的任何单元格样式),即使我这样做,文本标签的位置也完全重叠细节标签的位置。

I can see the detail text only if I include BOTH of the "required" lines above (and use any cell style other than Default), and even when I do that, the position of the text label completely overlaps the position of the detail label.

因此,似乎初始化程序正在创建detailTextLabel UILabel,但将其字体大小设置为零,并将其框架设置为空或屏幕外的内容。 (我已经尝试在调试器中检查这些,但它不是很有用 - 字体大小为零,框架为空,对于textLabel和detailTextLabel,但textLabel总是显示正常。)

So it appears as though the initializer is creating the detailTextLabel UILabel, but setting its font size to zero and its frame to something empty or offscreen. (I've tried inspecting these in the debugger, but it's not very useful -- the font size is zero and the frame empty for BOTH the textLabel and detailTextLabel, yet the textLabel always shows up fine.)

显然,如果必须,我可以通过手动调整标签大小和字体来解决它。但是,在这种情况下,我必须这样做,这通常只是设置样式,文本和布局是自动的,这让我非常不安。我搜索了谷歌和堆栈溢出,但找不到任何类似问题的引用。有没有人知道这里发生了什么?

Obviously I can work around it if I have to, by manually adjusting the label sizes and fonts. But it greatly disturbs me that I'm having to do that in this case, when normally you just set the style and text and layout is automatic. I've searched the googles and the stack overflows, but can't find any reference to a similar problem. Does anybody have any idea what's going on here?

(在iOS 6.1下测试。)

(Testing under iOS 6.1.)

推荐答案

在尝试了所有这些之后,当我在Storyboard中将表格视图单元格样式设置为Subtitle时,字幕显示给我

After trying all of these things, when I set the table view cell style to "Subtitle" in Storyboard, the subtitle showed up for me

这篇关于为什么我的UITableViewCell没有以任何风格显示detailTextLabel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆