ios - 如何在 iOS、Objective C 中的 TableView 的每个部分中仅实现单个单元格选择

标签 ios objective-c uitableview didselectrowatindexpath sections

我有一个包含 5 个部分的 TableView ,并且我已将 TableView 选择设置为多个。每个部分都有不同的行数。我想要的是设置用户只能从每个部分中选择一个单元格(在我的表中用户可以选择任意数量的单元格)。 例如:来自 5 个部分的 5 个单元格。

应该不可能从任何部分中选择多个单元格。如果用户从同一部分选择另一个单元格,则应取消选择先前选择的单元格。我怎样才能做到这一点。这是 didSelectRowAtIndexPath 的示例实现。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


    HoteldetalcelloneTableViewCell *cellone = (HoteldetalcelloneTableViewCell *)[self.detailtableView cellForRowAtIndexPath:indexPath];
    HoteldetailcelltwoTableViewCell *celltwo = (HoteldetailcelltwoTableViewCell *)[self.detailtableView cellForRowAtIndexPath:indexPath];

    //I have implement for two sections to test.
    if(indexPath.section == 0)
    {

       HotelDetailsone *secone = [roomonearray objectAtIndex:indexPath.row];
       HoteldetailsforBooking *book = [HoteldetailsforBooking new];
        if([secone.offerallow isEqualToString:@"True"])
        {
            celltwo.selectedsignLabel.hidden = NO;

        }
        else
        {

            cellone.selectedsignLabelone.hidden = NO;

        }
//        [self.detailtableView reloadData];
      NSLog(@"price for room 1 : %@", secone.itempriceText);

    }
    else
    {
        HotelDetailsone *sectwo = [roomtwoarray objectAtIndex:indexPath.row];
        HoteldetailsforBooking *book = [HoteldetailsforBooking new];
        if([sectwo.offerallow isEqualToString:@"True"])
        {
            celltwo.selectedsignLabel.hidden = NO;

        }
        else
        {

            cellone.selectedsignLabelone.hidden = NO;

        }
        //        [self.detailtableView reloadData];
        NSLog(@"price for room 1 : %@", sectwo.itempriceText);

    }

}

最佳答案

您需要跟踪单元格的选择。因此,您需要将选定的 indexpath 存储在数组中。

ViewController.h 中声明这样的属性

@property(nonatomic,strong) NSMutableDictionary *selectionData;

现在在 ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.selectionData=[[NSMutableDictionary alloc]init];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mycell"];


    if ([self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section] ] != nil) {

        NSMutableArray *sectionData=[[self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.section]] mutableCopy];

        if (![sectionData containsObject:[NSNumber numberWithLong:indexPath.row]])
        {
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.numberlabel.text = @"2";
        }
        else
        {
            cell.numberlabel.text = @"***";
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }
    }
    else
    {
        cell.numberlabel.text = @"2";
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

  cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"selected section :%li ---> selected row :%li",(long)indexPath.section, (long)indexPath.row);
    [self handleSelectionForSection:indexPath.section row:indexPath.row];
    [self.tablev reloadData];

}

-(void)handleSelectionForSection:(long)sectionIndex row:(long)rowIndex
{


    if ([self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",sectionIndex] ] != nil) {

        NSMutableArray *sectionData=[[self.selectionData objectForKey:[NSString stringWithFormat:@"%ld",sectionIndex]] mutableCopy];

        if (![sectionData containsObject:[NSNumber numberWithLong:rowIndex]])
        {
            //removing previous selected rows
            [sectionData removeAllObjects];
            [sectionData addObject:[NSNumber numberWithLong:rowIndex]];

            [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]];
        }
        else
        {
            //cell you tapped is already selected,
            // you can deselect it by removing object

            //if you dont want to deselect it comment following lines
            [sectionData removeObject:[NSNumber numberWithLong:rowIndex]];

            [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]];
        }


    }
    else
    {
        //section key not available so we need to create it
        NSMutableArray *sectionData=[[NSMutableArray alloc]init];
        [sectionData addObject:[NSNumber numberWithLong:rowIndex]];

        [self.selectionData setObject:sectionData forKey:[NSString stringWithFormat:@"%ld",sectionIndex]];

    }

    NSLog(@"All Selection : %@",self.selectionData);

}

您的 numberOfRowsInSectionnumberOfSectionsInTableViewtitleForHeaderInSection 将保持不变。

如果您有任何疑问,请告诉我。

关于ios - 如何在 iOS、Objective C 中的 TableView 的每个部分中仅实现单个单元格选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38221297/

相关文章:

objective-c - NSInputStream 读取 : maxlength: returning way more bytes than maxlength

ios - UITableView configureCellForDisplay :forIndexPath: 中的断言失败

Swift - 项目未显示在我的带有自定义单元格的表格 View 中

ios - 在从 Firebase DB 获取结果的 UITableView 上实现搜索

objective-c - 如何关闭UIWebView水平弹跳

ios - 基于iOS版本的条件单元测试

iphone - 如何从ViewController调用按钮方法?

ios - 区分 UITableViewCell 上的单击和双击

ios - 在每行 3 个单元格布局中将 cornerRadius 添加到第一个和第三个 UICollectionViewCell

ios - 在 Swift 中访问函数外部的变量