I have a UITableView with multiple sections. Instead of the typical approach of using an array per section, I am using one array. Anyway, I am having trouble getting the current indexPath.row as if there was only one section in the tableview.
So pretty much what I am trying to do is as follows. If the section == 0, I just use the indexPath.row, but if the section > 0, I want to add up all the rows from the previous section and get the current row in the current section to get the total row number AS IF THE TABLEVIEW WAS ONLY ONE SECTION.
This is my current code and it just isn't working out, perhaps you guys will see what I am doing wrong. Please let me know:
if (indexPath.section == 0) {
currentRow = indexPath.row;
} else {
NSInteger sumSections = 0;
for (int i = 0; i < indexPath.section; i++) {
int rowsInSection = [activitiesTable numberOfRowsInSection:i] + 1;
sumSections += rowsInSection;
}
NSInteger row = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section].row + 1;
currentRow = sumSections + row;
}
first you do not need the first if because it would not run through the for-next. Then you are adding one which is not necessary and then you can just add the indexPath.row like that:
NSInteger sumSections = 0;
for (int i = 0; i < indexPath.section; i++) {
int rowsInSection = [activitiesTable numberOfRowsInSection:i];
sumSections += rowsInSection;
}
currentRow = sumSections + indexPath.row;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With