依次选择UITableView中的单元格

时间:2018-09-28 12:51:14

标签: ios objective-c uitableview

我有一个包含大量单元格的UITableView。我可以选择多个单元格,但我想要的是用户只能依次选择多个单元格。也就是说,如果用户选择了第二行,那么他/她只能选择第三个单元格,然后依次选择第四个单元格,不允许选择随机单元格。

这是我所做的:

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

1 个答案:

答案 0 :(得分:4)

如果您不想选择当前的索引路径,则可以实现tableview委托方法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;
}

快捷键:

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