Multidimensional array

Im looking for an example of multidimensional arrays. I have a set of thumbnails (e.g. 9) and a table view of 4 previews of rows giving me 3 rows. I want to create a new multidimensional array that will contain 3 rows in each row containing 4 arrays.

Ive looked at a lot of examples over the past 3 hours, but they all seem to suggest using C-style encoding, and I'm not sure how to start initialization or if I need to free up. Plus, they use it in tabular form, so I'm not sure if you need to use NSarray or a bad way to get away with a C-style array. Any suggestions are greatly appreciated.

thumbnailarr[0][0] = 'img1.png';
thumbnailarr[0][1] = 'img2.png';
thumbnailarr[0][2] = 'img3.png';
thumbnailarr[0][3] = 'img4.png';

thumbnailarr[1][0] = 'img5.png';
thumbnailarr[1][1] = 'img6.png';
thumbnailarr[1][2] = 'img7.png';
thumbnailarr[1][3] = 'img8.png';

thumbnailarr[2][0] = 'img9.png';
0
source share
3 answers

, NSArray NSArrays . :

NSArray *thumbs= [NSArray arrayWithObjects:
                          [NSArray arrayWithObjects: @"img1.png",@"img2.png",@"img3.png",@"img4.png",nil],
                          [NSArray arrayWithObjects: @"img5.png",@"img6.png",@"img7.png",@"img8.png",nil],
                          [NSArray arrayWithObject: @"img9.png"],nil];

:

[[thumbs objectAtIndex:i] objectAtIndex:j]; //same as thumbs[i][j]
+2

objective-C , .

, : "" NSIndexPath.

NSUInteger nRows = 4;
NSUInteger nCols = 3;

-(NSInteger)indexForIndexPath:(NSIndexPath *)indexPath
{
    // check if the indexpath is correct with two elements (row and col)
    if ([indexPath length]!= 2) return -1;
    NSUIntegers indexes[2];
    [indexPath getIndexes:indexes];
    return indexes[0]*nCols+indexes[1];
}

-(NSIndexPath *)indexPathForIndex:(NSInteger)index
{
    NSInteger indexes[2];
    NSInteger indexes[0] = index/nCols;
    NSInteger indexes[1] = index%nCols;
    return [NSIndexPath indexPathWithIndexes:indexes length:2]
}
+4

Objective-C . C 2D-, NSArray NSArray.

NSString *thumbnailarr[3][4];

// initialize is easy if you include row-column in image names
// like img10.png instead of img5.png, img01.png instead of img2.png
for (NSInteger i = 0; i < 3; i++) {
    for (NSInteger j = 0; j < 4; j++) {
        thumbnailarr[i][j] = [[NSString alloc] initWithFormat:@"img%d%d.png", i, j];
    }
}

// in dealloc release them
for (NSInteger i = 0; i < 3; i++) {
    for (NSInteger j = 0; j < 4; j++) {
        [thumbnailarr[i][j] release];
    }
}

And to represent the table, the number of rows is what you return from the method tableView:numberOfRowsInSection:. It doesn't matter if you return an NSArray or a hard coded integer. This means that if you return 3 from this method, then there will be 3 cells. There is no special dependency on NSArray.

+1
source

All Articles