我有以下代码,它应该将图像放在顶部部分/单元格中,然后将文本放在其他部分/单元格中。我认为问题是当单元格离开屏幕时试图重用该单元格的代码,但我无法弄清楚我应该更改什么。你可以在这里看到问题的视频:
http://screencast.com/t/OoQWMI5zCKVc
代码如下:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifierText = @"TextCell";
static NSString *CellIdentifierImage = @"ImageCell";
if (indexPath.section == 0)
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierImage];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierImage] autorelease];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,180)];
imageView.tag = 1;
imageView.image = [UIImage imageNamed:@"prem.jpg"];
[cell addSubview:imageView];
[imageView release];
}
return cell;
}
else {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierText];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifierImage] autorelease];
}
if(indexPath.section == 1)
cell.textLabel.text = [arryTableData objectAtIndex:indexPath.row];
else if(indexPath.section ==2)
cell.textLabel.text = [arryAddressInfo objectAtIndex:indexPath.row];
else if(indexPath.section == 3)
cell.textLabel.text = [arryTableActions objectAtIndex:indexPath.row];
else if(indexPath.section == 4)
cell.textLabel.text = @"REPLACE ME WITH ICONS";
return cell;
}
}
最佳答案
当您创建将显示文本的 UITableViewCell
的新实例时,您使用了错误的标识符:
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:**CellIdentifierImage**] autorelease];
这会导致您的其中一个单元格被文本填充(但没有图像)被 UITableView
缓存为 CellIdentifierImage
标识符。因此,当您(在第 0 节中)询问具有该标识符的单元格时,您实际上得到的是一个仅填充有文本的单元格。
关于iphone - 没有替换图像和单元格的 UITableView 重绘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7994409/