在UITableViewController的单元格中更改图像

时间:2013-09-06 03:35:10

标签: ios objective-c uitableview

我有四个不同的单元格,我正在尝试添加图像,每次更改图像时,它都会将图像添加到每个单元格。我正在尝试为每个单元格添加单独的图像,但我不知道该怎么做。任何帮助都会很棒!这是我的代码:

if ([singletonObj.tempChangelistColor isEqualToString:@"red"]) {

            self.layer.contents = (id)[UIImage imageNamed:@"red.png"].CGImage;

        }else if([singletonObj.tempChangelistColor isEqualToString:@"yellow"]){

          self.layer.contents = (id)[UIImage imageNamed:@"yellow.png"].CGImage;

        }else if([singletonObj.tempChangelistColor isEqualToString:@"green"]){

            self.layer.contents = (id)[UIImage imageNamed:@"green.png"].CGImage;
        }else{

            self.layer.contents = (id)[UIImage imageNamed:@"shop_deal.png"].CGImage;

        }

它位于代码的底部,带有 shop_deal.png

2 个答案:

答案 0 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    if (indexPath.row == 0) {
        cell.imageView.image = [self getImageForCell];
    }
    else if (indexPath.row == 1) {
        cell.imageView.image = [self getImageForCell];
    }
    else if (indexPath.row == 2) {
        cell.imageView.image = [self getImageForCell];
    }
    else (indexPath.row == 3) {
        cell.imageView.image = [self getImageForCell];
    }
    return cell;
}
- (UIImage *)getImageForCell{

    UIImage *returnImage;

    if ([singletonObj.tempChangelistColor isEqualToString:@"red"]) {
        returnImage = [UIImage imageNamed:@"red.png"];
    }
    else if ([singletonObj.tempChangelistColor isEqualToString:@"yellow"]){
        returnImage = [UIImage imageNamed:@"yellow.png"];
    }
    else if([singletonObj.tempChangelistColor isEqualToString:@"green"]){
        returnImage = [UIImage imageNamed:@"green.png"];
    }
    else{
        returnImage = [UIImage imageNamed:@"shop_deal.png"];
    }
    return returnImage;
}

答案 1 :(得分:0)

如果你确切知道细胞没有。您想要更改图像,这将有助于您。

NSIndexPath *cellIndex = [NSIndexPath indexPathForRow:3 inSection:0]; // suppose cell no is 3 in section 0
UITableViewCell *cell = [_youtTableView cellForRowAtIndexPath:cellIndex];

cell.imageView.image = [UIImage imageNamed:@"shop_deal.png"];

希望这会有所帮助。

相关问题