ONE ARRAY - MULTIPLE SECTIONS – NSINDEXPATH

Tarun Kumar

Total Post:214

Points:1642
Posted by  Tarun Kumar
 735  View(s)
Ratings:
Rate this:

I have one big array with many values. I've sorted out the sections but I can't seem to get the indexpath right.

Basically, what I'm trying to do, is that if the previous section has 50 rows, the next indexpath.row should be 51->. The one after that should be 60-> and so fourth.

NSLog(@"current indexpath.row: %ld",(long)indexPath.row);

NSLog(@"current section: %ld",(long)indexPath.section);

for (int i = indexPath.section; i == indexPath.section; i++) {

            NSLog(@"number of rows: %ld",(long)[self tableView:tableView

numberOfRowsInSection: indexPath.section]);

            NSLog(@"in section: %ld",(long)indexPath.section);

 

    if (i++) {

        // add previous number of rows plus current

        int previousRows = [self tableView:tableView numberOfRowsInSection:

indexPath.section*2];

        NSLog(@"now indexpath: %ld",(long)previousRows+indexPath.row);

    }

}

 

I hope I've made my question clear enough for you to understand.

  1. Jayne Spooner

    Post:21

    Points:147
    Re: One Array - Multiple sections – NSIndexPath

    NSIndexPath doesn't work this way. NSIndexPath starts from 0 for every section.

     

    If I understand correctly, you want to know the global index value to access the array. For that case, I have been using this little method:

    - (int)getGlobalRowFor:(int)row at:(int)section {

        if(section == 0) {

            return row;

        } else {

            int count = 0;

            for(int i = 0; i < section; i++) {

                // This is where you put your own numbers for section counts.

                // This is my model, yours could be different.

                count += [[[self.sections objectAtIndex:i] objectForKey:@"sectionCount"]

    intValue];

            }

            return count + row;

        }

    }

      Modified On Jul-27-2015 07:51:09 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!