2013-03-15 82 views
4

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

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

我的主要手段归结为:

- (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以外的任何单元格样式) ,甚至当我这样做时,文本标签的位置与细节标签的位置完全重叠。

因此,看起来好像初始化程序正在创建detailTextLabel UILabel,但将其字体大小设置为零,并将其框架设置为空或离屏。 (我试图在调试器中检查这些,但它并不是非常有用 - 字体大小为零,框架为空textBabel和detailTextLabel,但textLabel总是显示正常)。

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

(测试的iOS 6.1下。)

+0

您使用的故事板?如果是这样,您需要将该属性设置为单元格中的字幕。这应该得到detailTextLabel显示。我以前遇到过这个问题。 – 2013-03-15 04:00:50

+0

不,没有故事板或XIB。只是代码。 – 2013-03-15 04:03:42

+0

而且,当我说我可以通过更改框架解决它时...我可以更改detailTextLabel.frame,但是我所有尝试更改textLabel.Frame都没有效果。在cellForRowAtIndexPath和tableView中都是true:willDisplayCell:forRowAtIndexPath。单元格本身似乎与textLabel有关,并完全忽略了detailTextLabel。 – 2013-03-15 04:05:10

回答

1

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

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

如字体大小,背景颜色改cell.labels这些所有的功能,你的操作在这个下面的方法中必须做。

- (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中更改这些类型的属性,则此方法将丢失这些属性。

+0

谢谢你试图帮忙。但正如在对原始问题的评论中指出的那样,更改textLabel的框架无论是在cellForRowAtIndexPath还是在willDisplayCell中都无效。而且,正如它发生的那样,改变detailTextLabel *的框架*可以在任何地方工作。 – 2013-03-15 14:13:19

3

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

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

不用说,这不是大多数人都会遇到的问题。但我想我会在这里为后人的缘故发布答案。

16

尝试所有的这些事情,当我设置在故事板的表视图单元格样式为“副标题”后,字幕显示为我IB Storyboard Cell setting

+0

这工作! upvoted! – kamyFC 2017-12-28 13:29:48

5

注册你使用风格UITableViewCells因为这个代码的标准UITableViewCell类,防止块将永远不会被执行

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

解决方案:你注册一个笔尖或类您的tableView?如果您有类似下面的线,你应该删除

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

和更新您的离队声明,就像如下

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMCControlsCellIdentifier]; 
if (cell == nil) { 

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kMCControlsCellIdentifier]; 
} 
0

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

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

含义

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

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

解决这个问题的唯一方法是将细胞从故事板寄存器或子类的UITableViewCell并重写

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

方法。

这个工作对我来说:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if (self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseIdentifier]) { 

    } 
    return self; 
} 
相关问题