为什么我的UITableViewCell不能在任何样式中显示detailTextLabel?

这个让我把头发拉出来。 我只是试图创build一个UITableViewCellStyleSubtitle风格的表格,文字和细节。 但细节没有显示出来。 通常这是因为有人忘记了用正确的样式来初始化单元格,但在这种情况下不是这样。 我已经尝试了所有四种单元格样式,并且其中的任何一个都没有显示细节,但是当我使用UITableViewCellStyleValue2时,文本标签确实移到了右侧。

我已经成功创build了多次表。 在这种情况下,我能想到的唯一明显的区别是我没有使用UITableViewController。 相反,我像任何其他视图一样embeddedUITableView,并连接我自己的数据源和委托。

我已经find了关键的方法:

- (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; } 

只有当我包含上面“必需”行的两个字符(并且使用除“默认”以外的任何单元格样式)时,才能看到详细信息文本,甚至当我这样做时,文本标签的位置也完全与细节标签的位置重叠。

所以看起来好像初始化器正在创builddetailTextLabel UILabel,但将其字体大小设置为零,并将其框架设置为空或离屏。 (我试过在debugging器中检查这些,但它不是非常有用 – 字体大小为零,框架为textLabel和detailTextLabel,但textLabel总是显示正常。)

显然,如果必须手动调整标签大小和字体,我可以解决这个问题。 但是在这种情况下,我不得不这样做,因为通常情况下,您只需设置样式,文本和布局是自动的。 我search了Google和堆栈溢出,但找不到任何类似的问题的参考。 有没有人知道这里发生了什么?

(在iOS 6.1下testing)

尝试所有这些事情之后,当我在Storyboard中将表格视图单元格样式设置为“Subtitle”时,字幕显示给我 IB故事板电池设置

注册一个标准的UITableViewCell类可以防止你使用样式化的UITableViewCells,因为这个代码块永远不会被执行

 if (not cell) { // .... Cell will never be nil if a class/nib is registered } 

解决scheme :你注册一个笔尖或类到你的tableView? 如果您有以下一行,则应删除

 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kMCControlsCellIdentifier]; 

并更新您的出列声明,如下所示

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMCControlsCellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kMCControlsCellIdentifier]; } 

好的,事实certificate,我们在这个应用程序上有一个非常不寻常的构build过程,它不能正确识别应用程序针对Cocoa框架构build的SDK。

因此,Cocoa认为这是一个非常古老的应用程序 – 从iOS4以前的日子开始,在添加了detailTextLabel之前。 所以,框架试图通过模拟iOS4之前的行为(通过以几种方式隐藏detailTextLabel)来提供帮助。 在_UIApplicationLinkedOnOrAfter上设置一个断点,强制它返回1,允许我绕过这个特性并certificate它是原因。 (现在我们只需要修复我们的构build过程来报告正确的SDK版本。)

不用说,这不是大多数人会遇到的事情。 但是我想我会在这里为后人的缘故贴出答案。

我希望这会帮助你,一旦检查

 - (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] autorelease]; } cell.textLabel.text = @"Foo"; cell.detailTextLabel.text = @"Bar"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.frame = CGRectMake(10, 20, 100,22); return cell; } 

像字体大小,单元格的背景颜色变化等操作。在下面的这个方法中,你需要做的所有的function。

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10]; cell.textLabel.backgroundColor=[UIColor redColor]; cell.textLabel.frame = CGRectMake(10, 20, 100,22); cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); } 

如果您正在更改cellForRowAtIndexPath这种方法的这些types的属性,那么你将失去这些属性。

问题是你最有可能注册你的细胞使用

 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"]; 

含义

 if (not cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease]; } 

将永远不会被调用,因为单元格将永远不会为空。 因此,单元格将始终由表格初始化为UITableViewCellStyleNone。

解决这个问题的唯一方法是从故事板中注册单元格或者inheritanceUITableViewCell并覆盖

 (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

方法。

这适用于我:

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseIdentifier]) { } return self; } 
Interesting Posts