ios - 按顺序在 UITableView 中多选单元格

标签 ios objective-c uitableview

我有一个包含多个单元格的 UITableView。我可以选择多个单元格,但我想要的是用户只能按顺序选择多个单元格。也就是说,如果用户选择了第 2 行,那么他/她只能依次选择第 3 个单元格和第 4 个单元格,不允许选择随机单元格。

这是我所做的:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return arrPackages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PurchaseListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PurchaseListTableViewCell" forIndexPath:indexPath];

    NSDictionary *dict = [arrPackages objectAtIndex:indexPath.row];
    cell.lblPackageName.text = [dict valueForKey:@"package_name"];
    cell.lblDiscount.text = [NSString stringWithFormat:@"You save: %@%%",[dict valueForKey:@"discount"]];
    cell.lblLastAmount.text = [NSString stringWithFormat:@"%@",[dict valueForKey:@"package_price"]];

    NSString *strPaymentStatus = [NSString stringWithFormat:@"%@",[dict valueForKey:@"payment_status"]];
    if ([strPaymentStatus isEqualToString:@"2"]) {
        cell.contentView.userInteractionEnabled = NO;
    }
    else{
        cell.contentView.userInteractionEnabled = YES;
    }
    /*
     NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:@"Your String here"];
     [attributeString addAttribute:NSStrikethroughStyleAttributeName
     value:@2
     range:NSMakeRange(0, [attributeString length])];
     */
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  //  NSDictionary *dict = [_arrModuleData objectAtIndex:indexPath.row];
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

最佳答案

您可以实现 TableView 委托(delegate)方法tableView(_:willSelectRowAt:)如果您不想选择当前索引路径,则返回 nil,否则您只返回给定的索引路径。

Objective-C :

-(NSIndexPath*)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray<NSIndexPath *> *selectedIndexPaths = [tableView indexPathsForSelectedRows];
    if (selectedIndexPaths == nil || selectedIndexPaths.count == 0) {
        return indexPath;
    }

    NSArray<NSIndexPath *> *sortedIndexPaths = [[selectedIndexPaths sortedArrayUsingComparator:^NSComparisonResult(NSIndexPath  * _Nonnull index1, NSIndexPath * _Nonnull index2) {
        return index1.row < index2.row;
    }] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat: @"section = %d", indexPath.section]];

    if (indexPath.row >= sortedIndexPaths[0].row &&
        indexPath.row <= sortedIndexPaths[sortedIndexPaths.count-1].row) {
        return indexPath;
    }

    return nil;
}

swift :

func tableView(_ tableView: UITableView, willSelectRowAt indexPath: IndexPath) -> IndexPath? {
    guard let selectedIndexPaths = tableView.indexPathsForSelectedRows,
        selectedIndexPaths.count > 0 else {
            return indexPath
    }

    let indexes = selectedIndexPaths
        .filter { $0.section == indexPath.section }
        .sorted()

    if let first = indexes.first,
        let last = indexes.last,
        indexPath.row >= first.row - 1 && indexPath.row <= last.row + 1 {
        return indexPath
    }

    return nil
}

当然,您应该为 tableView(_:willDeselectRowAt:)

编写类似的代码

关于ios - 按顺序在 UITableView 中多选单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52555759/

相关文章:

ios - 是否可以使用 Storyboard创建非全屏 UITableView?

ios - UITableView 部分始终保持在 View 中

ios - 当我单击 Objective-C 中的一个按钮(添加按钮)时,如何创建一组按钮?

ios - 标签值不变

objective-c - iCloud 和 objective-c : evictUbiquitousItemAtURL does not appear to actually remove the local file, 即使它返回 true

ios - 通过导航 Controller ios 按下后退按钮时刷新主页

ios - 在 iOS 中清除缓存目录的最佳做法是什么?

ios - 以编程方式调用 push segue 和 prepareforsegue 方法

IOS:如何仅使用点击手势即可允许触发的转场和发送的 Action

iphone - UITableView 不显示解析的数据