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