Aleph72
Aleph72

Reputation: 887

Get the selected cell number in a page

I have a tableview that always shows 3 cells for each page.

I need to know if the selected cell is the first, the second or the third of the page.

I'm sure there's a formula to calculate that, but I'm not able to find it.

Edit: let's see if I can make myself clearer.

Imagine to have an array of 8 objects to show in a tableview. Because the tableview is paged, the first page will show the indexes 0, 1, 2 of the array, the second page will show the indexes 3, 4, 5, and the last page will show the indexes 6 and 7.

Let's imagine we're on the second page (indexes 3, 4 and 5 shown) and I select the second cell (index 4).

I need to know that the user has selected the second cell, and not the indexPath.row that in this case is 4.

Upvotes: 0

Views: 93

Answers (2)

Aleph72
Aleph72

Reputation: 887

This is what I came up with. Many thanks to @Zaphod for the help.

Here I'm getting the indexPaths for the visible cells:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *visibleCells = [[NSArray alloc] initWithArray:[_tableView indexPathsForVisibleRows]];
    NSLog(@"Cell n. %d", [self indexOnPage:(int)indexPath.row idx:(NSArray*)visibleCells total:(int)[_licenses count] itemsOnPage:3]);
}

Then, to check if I'm on the last page, I've changed a little @Zaphod's solution to check if the last object is being shown:

-(int)indexOnPage: (int)row idx:(NSArray*)idx total:(int)total itemsOnPage:(int)itemsOnPage {
    NSIndexPath *lastIdx = [idx lastObject];
    int lastRow = (int)lastIdx.row;
    if (lastRow != [_licenses count]-1) {
        _selectedCell = row % itemsOnPage;
        return row % itemsOnPage;
    } else {
        int delta = total % itemsOnPage;
        _selectedCell = (row - delta) % itemsOnPage;
        return (row - delta) % itemsOnPage;
    }
}

Finally this method always returns 0 if the first visible cell is selected, 1 if the second cell is selected and 2 if the third cell is selected.

Upvotes: 0

Zaphod
Zaphod

Reputation: 7290

You just have to apply the modulo operator:

let indexOnThePage = indexPath.row % 3

indexOnThePage will be 0 for the first, 1 for the second, and 2 for the third.

4 % 3 == 1 // That means the second cell of the page

Bonus: If you want the page number, just do:

let pageNumber = indexPath.row / 3

It'll give 0 on the first page, 1 on the second, and so on.

EDIT: Here is the the version with you last page problem.

func indexOnPage(row: Int, page: Int, total: Int, itemsOnPage: Int = 3) -> Int {
    let lastPage = total / itemsOnPage

    if page != lastPage {
        return row % itemsOnPage
    }
    else {
        let delta = total % itemsOnPage
        return (row - delta) % itemsOnPage
    }
}

indexOnPage(row: 0, page: 0, total: 5) // 0
indexOnPage(row: 1, page: 0, total: 5) // 1
indexOnPage(row: 2, page: 0, total: 5) // 2
indexOnPage(row: 2, page: 1, total: 5) // 0
indexOnPage(row: 3, page: 1, total: 5) // 1
indexOnPage(row: 4, page: 1, total: 5) // 2

This should work for your problem.

Upvotes: 1

Related Questions