How to add multiple pictures to custom tableviewcells with tag or identifier

I want to add pictures from an NSMutableArray to custom cells in a tableview. There will be four or more images in every cell. So here's my problem:

If I refer to indexPath.row the custom cells would look like these:

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

but I want:

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

I'm new to xCode and I don't see a good solution.

Code:

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

Answers


Your problem is that while indexPath.row increases by one per call to tableView:cellForRowAtIndexPath: you are coding as if it increased by 4. Of course, UITableView row index increments should and will remain at one per row, so you have to find a different approach.

You need to find a function that maps indexPath to the index in the imagePathArray corresponding to the image you want to put leftmost in the cell for the the row described by indexPath. Once you find that index, the remaining three images are just 1, 2, and 3 elements offset from that.

Since this isn't tagged "homework" I guess I'll just give you the answer: it's the row multiplied by the number of images per row. You can take that as you like, or use this bit of code. Hasn't been compiled or tested, if you can't work out any typos or bugs on your own let me know.

Implement a method like this:

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

Then change the lines where you set your cell images in 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]];

Hope this helps!


Need Your Help

Function returning object array of runtime determined enum types

c# reflection

So I have a bunch of enum types (much more than this)

chrome for mobile ignores height 100%

javascript html css google-chrome

I have this html: http://jsfiddle.net/nsabu/

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.