简体   繁体   中英

How do I get the count of number of times user didSelectRowAtIndexPath?

On my UITableView I want to check how many times user has tapped the same row.

If user has tapped the same row three times I want to delete the row from the UITableView.

Can anyone please lead me to the solution? I tried doing:

for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
    count = count + 1;
    NSLog(@"rowCount %d indexPath.row %@", count, indexPath);
}

But this doesn't increment the count number of times the row was tapped by user.

Create an NSMutableDictionary property, where the key's are the indexPath's and the value is the current count of taps. eg

@property (nonatomic,strong) NSMutableDictionary *rowTaps;

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

if (!self.rowTaps) {
  self.rowTaps = [NSMutableDictionary dictionary];
}

[self.rowTaps setObject:[NSNumber numberWithInt:[(NSNumber *)[self.rowTaps objectForKey:indexPath]intValue]] forKey:indexPath];

if ([(NSNumber *)[self.rowTaps objectForKey:indexPath]intValue] == 3) {
     // Perform Delete Action - Delete row, and update datasource
}
}

Perform your check on the dictionary each time the selection occurs on a cell, then perform the necessary action.

Assuming you want to delete the row only if the user select three times a row the same cell.

Create another variable lastSelectedRow where you keep the last selected row. (create underneath the implementation line)

@implementation myViewController
{
   NSInteger = lastSelectedRow;
}

Next, you should verify that the row is equal to the last one selected, increment and check if the row have to be deleted:

for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {

    // If the last selected row is the same, increment the counter, else reset the counter to 1
    if (indexPath.row == lastSelectedRow) count++;
    else
    {
      lastSelectedRow = indexPath.row;
      count = 1;
    }

    // Now we verify if the user selected the row 3 times and delete the row if so
    if (count >= 3) [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

    NSLog(@"rowCount %d indexPath.row %@", count, indexPath);
}

Hope it helps.

Create the following property in the interface:

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (assign,nonatomic) int counter;

@end

In your implementation you can increment the counter as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.counter++;
    NSLog(@"the count is %i",self.counter);
}

Create a NSMutableDictionary and set the key as the cell index, then for the value set the count (1 at first). When the count reaches three then you can do what you need.

 //fake cell index (from indexpath)
    NSNumber * pretendCellIndex = [NSNumber numberWithInt:4];

    //Dict to track ocurrences
    NSMutableDictionary *index = [[NSMutableDictionary alloc]init];

    //If the dict has the index, increment
    if ([index objectForKey:pretendCellIndex]) {

        //Get value for the index
        int addOne = [[index objectForKey:pretendCellIndex] intValue];
        addOne++;

        //Add back to index
        [index setObject:[NSNumber numberWithInt:addOne] forKey:pretendCellIndex];

        //Your condition
        if (addOne>=3) {
            //do what you need
        }
    }else{
    //Havent seen so add
        [index setObject:[NSNumber numberWithInt:1] forKey:pretendCellIndex];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM