0

Im looking for an example of multidimensional arrays. I have an array of thumbtails (9 for example) and a tablecellview of 4 thumbs pre row, giving me 3 rows. I want to create a new multidimensional array which will hold the 3 rows each row containing 4 arrays.

Ive looked at many example for the past 3h but they all seem to suggest to use C-style coding and im not sure how to go about initialising them or if i need to release. Plus im using this in a tableview so im not sure if ill need to use NSarray or ill be able to get away with C-style array. Any suggestions gratefully 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';
Vladimir
  • 170,431
  • 36
  • 387
  • 313
david-l
  • 623
  • 1
  • 9
  • 20
  • Does this answer your question? [Creating a two dimensional array in Objective-C](https://stackoverflow.com/questions/4362740/creating-a-two-dimensional-array-in-objective-c) – ggorlen Jan 13 '22 at 05:36

3 Answers3

4

There is no special multidimensional array in objective-C but you could work with a one-dimensional array, too.

You would then use modulo-based calculations to convert the desired row and column index to the array index: You would use an NSIndexPath to describe the "coordinates" in your multidimensional array.

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]
}
GorillaPatch
  • 5,007
  • 1
  • 39
  • 56
3

A multidimensional array is essentially an array of arrays, an NSArray can have NSArrays as its contents. For example:

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

access like this:

[[thumbs objectAtIndex:i] objectAtIndex:j]; //same as thumbs[i][j]
Michael Behan
  • 3,433
  • 2
  • 28
  • 38
  • I decided not to go with C-style only because it would have required changing many other things in my code. I didn't do exactly what you suggested mbehan but it did lead me to another post http://classroomm.com/objective-c/index.php?topic=3260.5;wap2 which involved less typing and it didn't matter how many images i was dealing with. Thank you all for you're suggestions. – david-l Apr 06 '11 at 12:12
1

Objective-C does not have anything special for multidimentional array. You need to use C 2D array, unless you want to use NSArray of 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 for the table view, row count is what you return from the tableView:numberOfRowsInSection: method. It does not matter whether you return a NSArray count or hard coded integer. That means if you return 3 from this method then there will be 3 cells. There is no special dependency on NSArray.

taskinoor
  • 45,586
  • 12
  • 116
  • 142