静态表格视图适用于iOS6,但不适用于iOS5
我已经搜索了几个小时,发现了许多提示,但没有找到解决方案。我有一个非常简单的静态单元格tableview。我正在关注Apple文档。我没有使用storyboard,我没有选择自动布局,将文档版本设置为iOS5。它在iOS6中工作得很好,但iOS5中的单元格为NIL,因此它会崩溃。静态表格视图适用于iOS6,但不适用于iOS5
细胞在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中工作。
如果(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;
}
当你使用dequeueReusableCellWithIdentifier:它可以返回零,如果没有产生细胞。您需要在此时分配并初始化您自己的单元格。
布莱斯,我想用我的上面的代码。这就是苹果在他们的非故事板静态表格视图文件中写入的内容。如上所述,尽管我完全按照文档所述的内容,但在iOS6中完美工作,但足够有趣。我想这是一个问题,我使用了一个后来的xcode,而不是Apple在其早期文档中使用的版本。所以有些东西没有正确连接。在模拟器和iOS6设备上运行良好,在iOS5上使用空单元时崩溃 – user2373373 2013-05-14 01:35:25
我猜你会在模拟器中看到同样的问题,如果你开始模拟内存警告。如果你认为这是一个“在布线中”的错误。我建议使用MARTIN的建议,而不是使用故事板。 – 2013-05-30 18:00:02
Martin,感谢您的回复。不幸的是,我得到完全相同的错误消息,NSLOG为单元格的描述返回null。 – user2373373 2013-05-12 16:06:25
UITableViewCell * cell1 = [tableview dequeueReusableCellWithIdentifier:@“cell1”];将此方法替换为UITableViewCell * cell1 = [tableview dequeueReusableCellWithIdentifier:@“cell1”forIndexPath:indexpath.row];或者UITableViewCell * cell1 = [tableview dequeueReusableCellWithIdentifier:@“cell1”forIndex:index]; //我不知道确切的方法。尝试ATLEAST这一个 – MARTIN 2013-05-12 16:16:34
我知道这种方法的iOS5和6之间改变不过,我不应该需要任何可重复使用的细胞在静态视图没有什么可以重复使用(所有自定义细胞是不同的) – user2373373 2013-05-12 16:27:31