1

NSMutableArrayテーブルビューのカスタムセルに画像を追加したい。すべてのセルに4つ以上の画像があります。だからここに私の問題があります:

カスタムセルを参照するとindexPath.row、次のようになります。

cell1:picture1、
picture2
、picture3、picture4 cell2:picture2、picture3、picture4、picture5 cell3:picture3、picture4、picture5、picture6

でも私はしたい:

cell1: picture1、picture2、picture3、picture4 cell2:picture5
、picture6、picture7、picture8
cell3:picture9、 picture10、picture11、picture12

私は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;
}
4

1 に答える 1

1

問題は、indexPath.row呼び出しごとに1ずつ増加する一方でtableView:cellForRowAtIndexPath:、4ずつ増加したかのようにコーディングしていることです。もちろん、UITableView行インデックスの増分は行ごとに1のままである必要があるため、別のアプローチを見つける必要があります。

で記述された行のセルの左端に配置する画像に対応するindexPathインデックスにマップする関数を見つける必要があります。そのインデックスを見つけると、残りの3つの画像は、そこからオフセットされた1、2、および3つの要素になります。imagePathArrayindexPath

これは「宿題」のタグが付いていないので、答えをお伝えします。行に1行あたりの画像数を掛けたものです。あなたはそれを好きなように取ることができます、あるいはこのコードのビットを使うことができます。コンパイルもテストもされていません。タイプミスやバグを自分で解決できない場合は、お知らせください。

次のようなメソッドを実装します。

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

お役に立てれば!

于 2012-09-08T01:23:42.250 に答える