Search code examples
objective-cuiimageimageviewcell

Objective C imageView with index with tableCell


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
        formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    MovieSighting *sightingAtIndex = [self.dataController objectInListAtIndex:indexPath.row];
    [[cell textLabel] setText:sightingAtIndex.titulo];
    [[cell detailTextLabel] setText:[NSString stringWithFormat:@"%lu",(unsigned long)sightingAtIndex.anyo]];

    [[cell imageView] setImage:[UIImage imageNamed:@"Batman.jpg"] ];
    [[cell imageView] setImage:[UIImage imageNamed:@"300.jpg"]];


    //cell.imageView.image = [UIImage imageNamed:@"Batman.jpg"];
    //cell.imageView.image = [UIImage imageNamed:@"300.jpg"];

    //[[cell detailTextLabel] setText:[formatter stringFromDate:(NSDate *)sightingAtIndex.date]];
    return cell;
}

I would like to add the first image to the first cell

cell.imageView.image = [UIImage imageNamed:@"Batman.jpg"];

I would like to add the second image to the second cell

cell.imageView.image = [UIImage imageNamed:@"300.jpg"];

I should to do with atIndex?


Solution

  • Just check the index to know the cell where you are:

    if (indexPath.row == 0)
        [[cell imageView] setImage:[UIImage imageNamed:@"Batman.jpg"] ];
    else if (indexPath.row == 1)
        cell.imageView.image = [UIImage imageNamed:@"300.jpg"];
    

    That method is called ONCE for every cell it's going to be displayed, not once for all the cells.