0

试图在单元格内加载图像。我能够在单元格内加载图像,但是当上下滚动 tableView 时,图像也会随机出现在其他不同的单元格内......

这是一些代码:

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";


UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];

    cell.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];


}

if (indexPath.section == ONE_SECTION) {

    switch(indexPath.row) {

        case 0:{
            //some code
}
                break;
            case 2:{
                              //some code

}
                break;
            case 3:{


            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(256,0,43,43)];
            imageView.contentMode = UIViewContentModeScaleAspectFit;
            [cell.contentView addSubview:imageView];
            imageView.layer.cornerRadius = 10.0;
            //imageView.contentMode = UIViewContentModeScaleAspectFit;
            imageView.layer.masksToBounds = YES;
            imageView.image = [UIImage imageNamed:@"imageName.png"];}
            break;
        default:
            break;
    }

}

if (indexPath.section == TWO_SECTION) {
    //some code
}

if (indexPath.section == THREE_SECTION) {
    //some code
}


return cell;
}

谢谢你

4

2 回答 2

0

由于您使用的是缓存单元格,因此您获得的单元格可能是您在之前调用代码时添加了图像视图的单元格。如果您tag为您的图像视图分配一个唯一性,那么对于您不想要它的情况,您可以检查它的存在并在找到它时将其删除。

于 2012-12-03T22:55:51.150 回答
0

试试下面的代码。如果您正在重用单元格,则不必imageView重复添加到所有单元格。您可以在单元格分配部分创建它,然后在其外部设置imageView.image属性,如下所示。

- (UITableViewCell *)tableView:(UITableView *)tableView_ cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView_ dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];

    cell.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.textColor = [UIColor whiteColor];

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(256,0,43,43)];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [cell.contentView addSubview:imageView];
    imageView.layer.cornerRadius = 10.0;
    //imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.layer.masksToBounds = YES;
    imageView.tag = 567; //unique tag
}

UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:567];
imageView.image = nil;

if (indexPath.section == ONE_SECTION) {

    switch(indexPath.row) {

        case 0:{
            //some code
        }
        break;
        case 2:{
                              //some code
        }
        break;
        case 3:{

            UIImageView *imageView = (UIImageView *)[cell.contentView viewWithTag:567];
            imageView.image = [UIImage imageNamed:@"imageName.png"];
        }
            break;
        default:
            break;
    }
}

if (indexPath.section == TWO_SECTION) {
    //some code
}

if (indexPath.section == THREE_SECTION) {
    //some code
}

 return cell;
}

更好的方法是创建自定义UITableViewCell类并添加UIImageViewinit该单元格的方法中。然后将属性设置为cell.imageView.image = ...方法中cellForRowAtIndexPath

于 2012-12-03T23:00:50.950 回答