在单元格内添加uiimageview

时间:2012-12-03 22:32:23

标签: iphone xcode uitableview

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

这里有一些代码:

- (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;
}

谢谢

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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类,并在该单元格的UIImageView方法中添加init。然后在cell.imageView.image = ...方法中将属性设置为cellForRowAtIndexPath