简体   繁体   English

更改UITableViewController单元格中的图像

[英]Changing images in cells for UITableViewController

I have four different cells that I am trying to add images to and every time that I change the image it adds the image to each cell. 我尝试将四个不同的单元格添加到图像中,每次更改图像时,它会将图像添加到每个单元格中。 I'm trying to add separate images for each cell, but I'm not sure how to do it. 我正在尝试为每个单元格添加单独的图像,但是我不确定该怎么做。 Any help would be great! 任何帮助将是巨大的! Here's my code: 这是我的代码:

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;

        }

It's at the bottom of the code with the shop_deal.png . 它位于shop_deal.png的代码的底部

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

if you exactly know the cell no. 如果您确切知道细胞编号。 of which you want to change the image then this will help you. 您想更改其中的图像,那么对您有帮助。

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"];

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM