iphone - 没有替换图像和单元格的 UITableView 重绘

标签 iphone objective-c ios xcode uitableview

我有以下代码,它应该将图像放在顶部部分/单元格中,然后将文本放在其他部分/单元格中。我认为问题是当单元格离开屏幕时试图重用该单元格的代码,但我无法弄清楚我应该更改什么。你可以在这里看到问题的视频:

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/

相关文章:

iPhone 开发者门户不接受我的 CSR

c++ - Objective-C 是否存在 C+ +'s "const 方法的近似值?

objective-c - 如何在 Xcode 6 中禁用 ARC

ios - 无法识别的选择器发送到实例 resignFirstResponder

ios - 聚合 CMPedometer 数据(iPhone + watch 总数)

ios - 在iPhone的个人热点网络上查找设备

ios - 具有缩放比例模式的UIImageView显示奇怪的图像

ios - 用于获取健康属性的监视包或健康包

ios - UIViewControllers 之间的自定义转换不起作用

objective-c - 同时禁用多个长按手势