UITableViewCell宽度始终返回320

时间:2015-11-09 11:40:20

标签: ios

单元格宽度始终返回320,一旦重新加载表格,它就会根据设备(iphone / ipad)返回正确的单元格宽度。下面是代码

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

NSString *reuseIdentifier = @"testCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

if(cell == nil)

 {
     cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdentifier];

 }    

cell.textLabel.text = @"test";

return cell;

}

1 个答案:

答案 0 :(得分:0)

可能是因为第一次通过

创建了您的单元格
[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdentifier];

但重新加载表后,您的单元格将通过

重复使用
[tableView dequeueReusableCellWithIdentifier:reuseIdentifier]

带有“旧”尺寸。

要避免这种情况,请在“界面”构建器中使用正确的单元格标识符,在这种情况下,您甚至可以删除创建单元格代码

[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:reuseIdentifier]

将始终从接口

生成单元格