繁体   English   中英

如何获取用户didSelectRowAtIndexPath的次数?

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

在我的UITableView上,我想检查用户点击同一行的次数。

如果用户点击了同一行三次,我想从UITableView中删除该行。

有人可以请我解决这个问题吗? 我试过做:

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

但是这不会增加用户点击行的计数次数。

创建一个NSMutableDictionary属性,其中键是indexPath,值是当前的抽头计数。 例如

@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
}
}

每次在单元格上进行选择时,都会对字典执行检查,然后执行必要的操作。

假设您只想在用户选择三次相同单元格时删除该行。

创建另一个变量lastSelectedRow ,保留最后选定的行。 (在实施线下面创建)

@implementation myViewController
{
   NSInteger = lastSelectedRow;
}

接下来,您应该验证该行是否等于最后一行,递增并检查是否必须删除该行:

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);
}

希望能帮助到你。

在界面中创建以下属性:

@interface ViewController () <UITableViewDelegate,UITableViewDataSource>

@property (assign,nonatomic) int counter;

@end

在您的实现中,您可以按如下方式递增计数器:

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

创建一个NSMutableDictionary并将密钥设置为单元格索引,然后为值设置count(首先为1)。 当计数达到3时,你可以做你需要的。

 //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];
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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