【问题标题】:Multi-select cell in UITableView in sequenceUITableView中按顺序多选单​​元格
【发布时间】:2018-09-28 12:51:14
【问题描述】:

我有一个包含许多单元格的 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;
}

【问题讨论】:

  • 请展示您已经尝试过的内容、无效的内容、代码示例等。请阅读How to Askminimal reproducible example 然后edit 您的问题显示最小值 演示问题的代码。
  • @AshleyMills 感谢您的回复。我在这里更新了代码。

标签: ios objective-c uitableview


【解决方案1】:

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

目标-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;
}

斯威夫特:

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:)编写类似的行为

【讨论】:

  • 问题标记为 Objective-C。答案应使用适当的语言。
  • 谢谢你的回答,但我已经要求objective-c
  • 对不起,我没有注意标签。这是 Obj-C 的更新答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2011-09-04
相关资源
最近更新 更多