【问题标题】:static table view works in iOS6 but not iOS5静态表格视图适用于 iOS6 但不适用于 iOS5
【发布时间】:2013-05-11 17:40:42
【问题描述】:

我已经搜索了几个小时,发现了很多提示,但没有找到解决方案。我有一个非常简单的带有静态单元格的表格视图。我正在关注 Apple 文档。我没有使用故事板,我没有选中自动布局,将文档版本设置为 iOS5。它在 iOS6 中运行良好,但在 iOS5 中单元格为 NIL,因此会崩溃。

单元格在 IB 中完成,我已经连接了属性,我有单元格标识符(实际上不需要但没有区别)。日志语句在 iOS5 中返回 (null),但在 iOS6 中正确返回一个单元格。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0 ) {

        if (indexPath.row ==0) {

        cell0.selectionStyle = UITableViewCellSelectionStyleNone;
        NSLog(@"description = %@",[cell0 description]);
        return cell0;
        }
        if (indexPath.row ==1) {

        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell1;
        }
        if (indexPath.row ==2) {

            cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell2;
        }
        if (indexPath.row ==3) {

            cell3.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell3;
        }
    }
    cell4.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell4;
}

提前感谢您为我指明了正确的方向。我知道它一定很简单,但它让我发疯。

【问题讨论】:

    标签: ios5 ios6 uitableview


    【解决方案1】:

    将您的代码替换为以下代码 sn-p 它将在 ios5 中运行。

    if (indexPath.section == 0 ) {

        if (indexPath.row ==0) {
    
        UITableViewCell *cell0 = [tableview dequeueReusableCellWithIdentifier:@"cell0"];
    
        cell0.selectionStyle = UITableViewCellSelectionStyleNone;
        NSLog(@"description = %@",[cell0 description]);
        return cell0;
    
        if (indexPath.row ==1) {
    
        UITableViewCell *cell1 = [tableview dequeueReusableCellWithIdentifier:@"cell1"];
    
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell1;
        }
    
        if (indexPath.row ==2) {
    
        UITableViewCell *cell2 = [tableview dequeueReusableCellWithIdentifier:@"cell2"];
            cell2.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell2;
        }
    
       if (indexPath.row ==3) {
    
          UITableViewCell *cell3 = [tableview dequeueReusableCellWithIdentifier:@"cell3"];
            cell3.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell3;
        }
    
       UITableViewCell *cell4 = [tableview dequeueReusableCellWithIdentifier:@"cell4"];
       cell4.selectionStyle = UITableViewCellSelectionStyleNone;
       return cell4;
    

    }

    【讨论】:

    • 马丁,感谢您的回复。不幸的是,我收到完全相同的错误消息,并且 NSLOG 为单元格的描述返回 null。
    • UITableViewCell *cell1 = [tableview dequeueReusableCellWithIdentifier:@"cell1"];将此方法替换为 UITableViewCell *cell1 = [tableview dequeueReusableCellWithIdentifier:@"cell1" forIndexPath: indexpath.row];或 UITableViewCell *cell1 = [tableview dequeueReusableCellWithIdentifier:@"cell1" forIndex: index]; // 我不知道确切的方法。至少试试这个
    • 我知道这个方法在 iOS5 和 6 之间发生了变化。但是我不需要任何可重用的单元格,因为在静态视图中没有什么可以重用(所有自定义单元格都不同)
    【解决方案2】:

    当你使用 dequeueReusableCellWithIdentifier 时:如果没有创建单元格,它可以返回 nil。此时您需要分配并初始化您自己的单元格。

    【讨论】:

    • 布莱斯,我想使用我上面的代码。这就是 Apple 在他们的文档中为非故事板静态表视图所写的内容。如前所述,在 iOS6 中完美运行,但在 iOS5 中却不够有趣,尽管我完全按照文档所说的做了。我想这是一个问题,我使用的 xcode 比 Apple 在其旧文档中使用的版本更高。所以有些东西没有正确连接。在模拟器和 iOS6 设备上运行良好,在 iOS5 中的空单元格都崩溃
    • 我猜如果你开始模拟内存警告,你会在模拟器中看到同样的问题。如果您认为这是“布线中的”错误。我建议使用 MARTIN 的建议,而不是使用故事板。
    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多