Objective C uiimageView,索引与表格Cell

时间:2013-12-13 16:29:44

标签: objective-c uiimage imageview cell

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

我想将第一张图片添加到第一个单元格

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

我想将第二张图片添加到第二个单元格

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

我应该用atIndex做什么?

1 个答案:

答案 0 :(得分:1)

只需检查索引即可知道您所在的单元格:

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

对于要显示的每个单元格,该方法称为ONCE,而不是所有单元格都称为。