Balázs Vincze
Balázs Vincze

Reputation: 3832

How to number the rows in UITableView

I would like to number the rows in my app, just like in excel. I use a label for this in the cell. I have written some code which does this perfectly:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.numberlabel.text = [NSString stringWithFormat:@"%li.",(indexPath.row+1)];

    return cell;
}

However the problem is, that whenever I delete a row the numbering does not get refreshed. So I thought maybe I would iterate through every cell and set the number again, whenever the user deletes a row. Here is what I did:

NSArray *cells = [tableView visibleCells];

int n = 0;

for (TableViewCell *cell in cells)
{
     n++;
     cell.numberlabel.text = [NSString stringWithFormat:@"%i.",n];
}

This works great, but again I faced another problem: The tableview is reusing cells so I can not iterate through each and every one of them, only the ones that are actually on screen.

So my question is: How would I go about solving this issue of properly numbering each row in the TableView, even if the user starts deleting rows?

Upvotes: 2

Views: 1773

Answers (3)

Joe Hallenbeck
Joe Hallenbeck

Reputation: 1450

i think first method be great

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     cell.numberlabel.text = [NSString stringWithFormat:@"%li.",(indexPath.row+1)];

     return cell;
}

if user delete row, you simply can catch this and reload data and table view redraw with normal values

Upvotes: 1

JulianM
JulianM

Reputation: 2550

You have to do the following:

  1. Use your first approach by setting the label text in the cellForRowAtIndexPath method

  2. Delete your desired row

  3. Now do not reload the whole tableview but use reloadRowsAtIndexPaths for all visible cells. All offscreen cells will have the correct row number as soon as they become visible because it will be set in the cellForRowAtIndexPath method.

Example:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Default" forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%li", indexPath.row +1];
    return cell;

}
- (NSArray *) tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {

        self.numberOfRows--;

        NSMutableArray *visibleCellIndexPaths = [[tableView indexPathsForVisibleRows] mutableCopy];
        [visibleCellIndexPaths filterUsingPredicate:[NSPredicate predicateWithFormat:@"row > %i",indexPath.row]];

        [tableView beginUpdates];

        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView reloadRowsAtIndexPaths:visibleCellIndexPaths withRowAnimation:UITableViewRowAnimationFade];

        [tableView endUpdates];



    }];

    return @[deleteAction];

}

Example

Upvotes: 4

Vikas Dadheech
Vikas Dadheech

Reputation: 1720

If you don't want to reload the table after the deletion, then you can use the following approach: You can iterate through the cells. Iteration will start from the indexpath of the cell which recently got deleted to the number of rows. And you can reduce their number by one during iteration.

Upvotes: 0

Related Questions