静态表视图适用于iOS6但不适用于iOS5

时间:2013-05-11 17:40:43

标签: ios5 ios6 uitableview

我已经搜索了几个小时和几个小时,但发现了许多提示但不是解决方案。我有一个非常简单的静态单元格的tableview。我正在关注Apple文档。我没有使用故事板,我已经取消选中autolayout,将文档版本设置为iOS5。它在iOS6中运行得很好,但在iOS5中单元格是NIL,因此它会崩溃。

单元格在IB中完成,我有连接的属性,我有单元格标识符(实际上不需要但没有区别)。 log语句在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;
}

提前感谢您指出我正确的方向。我知道这一定很简单但却让我发疯。

2 个答案:

答案 0 :(得分:1)

将代码替换为以下可在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;

}

答案 1 :(得分:0)

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

相关问题