【问题标题】:How to add multiple pictures to custom tableviewcells with tag or identifier如何使用标签或标识符将多张图片添加到自定义 tableviewcells
【发布时间】:2012-09-07 22:59:23
【问题描述】:

我想将NSMutableArray 中的图片添加到表格视图中的自定义单元格中。每个单元格中将有四个或更多图像。所以这是我的问题:

如果我引用 indexPath.row,自定义单元格将如下所示:

单元格1:图片1、图片2、图片3、图片4
单元格 2:图片 2、图片 3、图片 4、图片 5
单元格3:图片3、图片4、图片5、图片6

但我想要:

单元格1:图片1、图片2、图片3、图片4
单元格2:图片5、图片6、图片7、图片8
单元格3:图片9、图片10、图片11、图片12

我是 xCode 的新手,我没有看到好的解决方案。

代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"ImageCustomCell";

    ImageCustomCell *cell = (ImageCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ImageCustomCell" owner:nil options:nil];

        for(id currentObject in topLevelObjects) {

            if ([currentObject isKindOfClass:[UITableViewCell class]]) {

                cell = (ImageCustomCell *) currentObject;

            break;
            }
        }
    }
// here i get the indexPath.row for every cell and add +1 - +3 to it to get the next image, but every new cell just get +1, not +4
    cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row]];
    cell.imageForCell2.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+1]];
    cell.imageForCell3.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+2]];
    cell.imageForCell4.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:indexPath.row+3]];
    return cell;
}

【问题讨论】:

    标签: ios uitableview uiimage nsmutablearray


    【解决方案1】:

    您的问题是,虽然 indexPath.row 每次调用 tableView:cellForRowAtIndexPath: 都会增加 1,但您的编码好像增加了 4。当然,UITableView 行索引增量应该并且将保持在每行一个,所以您必须找到不同的方法。

    您需要找到一个函数,将indexPath 映射到imagePathArray 中的索引,该索引对应于您要放置在indexPath 所描述行的单元格中最左侧的图像。找到该索引后,剩下的三张图像只是从该索引偏移 1、2 和 3 个元素。

    由于这没有标记为“作业”,我想我只会给你答案:它是行乘以每行的图像数量。您可以随意使用它,也可以使用这段代码。尚未编译或测试,如果您无法自行解决任何拼写错误或错误,请告诉我。

    实现这样的方法:

    - (NSArray *)imagePathsForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSInteger imagePathArrayStartingIndex = indexPath.row * IMAGES_PER_ROW;
        NSRange imagePathArrayIndexRange = NSMakeRange(imagePathArrayStartingIndex, IMAGES_PER_ROW);
        NSIndexSet *imagePathArrayIndexes = [NSIndexSet indexSetWithIndexesInRange:imagePathArrayIndexRange];
        NSArray *imagePathsForRow = [imagePathArray objectsAtIndexes:imagePathArrayIndexes];
        return imagePathsForRow;
    }
    

    然后更改您在tableView:cellForRowAtIndexPath 中设置单元格图像的行:

    NSArray *imagePathsForRow = [self imagePathsForRowAtIndexPath:indexPath];
    cell.imageForCell.image  = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:0]];
    cell.imageForCell2.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:1]];
    cell.imageForCell3.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:2]];
    cell.imageForCell4.image = [UIImage imageWithContentsOfFile:[imagePathsForRow objectAtIndex:3]];
    

    希望这会有所帮助!

    【讨论】:

    • 不客气!这有助于解决问题还是您仍需要进一步的帮助?
    • 是的,对我帮助很大。但它更简单: cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:(indexPath.row*1)]];和 cell.imageForCell.image = [UIImage imageWithContentsOfFile:[imagePathArray objectAtIndex:(indexPath.row*2)]];等等..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 2021-11-04
    • 2017-12-30
    • 1970-01-01
    相关资源
    最近更新 更多