6

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

我之前已经成功创建过很多次表格。在这种情况下,我能想到的唯一显着区别是我没有使用 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;
}

仅当我包含上面的“必需”行(并使用除默认值以外的任何单元格样式)时,我才能看到详细信息文本,即使我这样做,文本标签的位置也会与详细信息标签的位置完全重叠.

所以看起来好像初始化程序正在创建 detailTextLabel UILabel,但将其字体大小设置为零,并将其框架设置为空的或屏幕外的东西。(我试过在调试器中检查这些,但它不是很有用——字体大小为零,textLabel 和 detailTextLabel 的框架都是空的,但 textLabel 总是显示得很好。)

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

(在 iOS 6.1 下测试。)

4

5 回答 5

16

在尝试了所有这些事情之后,当我在 Storyboard 中将表格视图单元格样式设置为“字幕”时,字幕出现在我面前IB 故事板单元格设置

于 2013-09-16T17:23:44.330 回答
8

注册标准UITableViewCell类可能会阻止您使用 styled UITableViewCells,因为此代码将永远不会被执行:

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

解决方案:您是注册一个笔尖还是Class您的UITableView?如果您有如下一行,请尝试删除它:

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

并更新您的 dequeue 语句,如下所示:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:"cellReuseID"];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
                                  reuseIdentifier:"cellReuseID"];
}
于 2015-12-08T16:05:43.350 回答
3

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

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

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

于 2013-03-15T18:22:08.837 回答
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 此方法中更改这些类型的属性,那么您将丢失这些属性。

于 2013-03-15T04:31:57.943 回答
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;
}
于 2016-04-21T19:34:22.417 回答