• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 分组 UITableView - 允许多选和单选

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:31:49 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个从 NSArray 获取数据的 UITableView。该数组包含模型对象。我已将 UITableView 分成多个部分。现在我试图让某些部分可以多选,而其他部分只能单选。我的模型对象有一个属性,我用它来确定我需要多选还是单选。我快到了 - 我已经设法让多选和单选在正确的部分工作。这是代码:

-(void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes[indexPath.section];

    if ([productAttribute.filterType isEqualToString"MULTI_SELECT_LIST"]) {
        if (productAttribute.option[indexPath.row]) {
            [self.selectedRows addObject:indexPath];
        }
        else {
            [self.selectedRows removeObject:indexPath];
        }
    }

    [self.tableView reloadData];
}

为了解决重用问题,当某些单元格有复选标记时,即使它们没有被选中,我也会在我的 cellForForAtIndexPath: 方法中这样做:

if([self.selectedRows containsObject:indexPath]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

//For single selection 
else if (self.selectedIndexPath.row == indexPath.row &&
         self.selectedIndexPath.section == indexPath.section) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

我每个部分的选择都在正常工作。每个部分都允许多选或单选 - 取决于 didSelectRowAtIndexPath: 方法中的 if 语句。

问题是:

如果我在第 2 节中选择了一行,假设它是单选,然后我在第 3 节中选择了一行,也是单选,复选标记从第 2 节移动到第 3 节。

我需要第 2 节和第 3 节保持单选 - 但允许两者同时选择一行。所以它应该是这样的:

  • 第 1 部分多选):选中所有行
  • 第 2 部分单选):选中一行
  • 第 3 部分单选):选中一行

而不是这个,当我从第 2 部分中选择一行时,它看起来像这样:

  • 第 1 部分多选):选中所有行
  • 第 2 部分单选:已删除先前的选择
  • 第 3 部分单选):选中一行



Best Answer-推荐答案


将您的 didSelectRowAtIndexPath: 更改为

- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    BBFilterProductAttribute *productAttribute = self.productAttributes[indexPath.section];

    if ([productAttribute.filterType isEqualToString"MULTI_SELECT_LIST"])
    {
        if (productAttribute.option[indexPath.row])
        {
            [self.selectedRows addObject:indexPath];
        }
        else
        {
           [self.selectedRows removeObject:indexPath];
        }
    }
    else
    {
        //Section is SINGLE_SELECTION
        //Checking self.selectedRows have element with this section, i.e. any row from this section already selected or not
        NSPredicate *predicate = [NSPredicate predicatewithFormat"section = %d", indexPath.section];
        NSArray *filteredArray = [self.selectedRows filteredArrayUsingPredicate:predicate];
        if ([filteredArray count] > 0)
        {
            //A row from this section selected previously, so remove that row
            [self.selectedRows removeObject:[filteredArray objectAtIndex:0]];
        }

        //Add current selected row to selected array
        [self.selectedRows addObject:indexPath];
    }

    [self.tableView reloadData];    
}

关于ios - 分组 UITableView - 允许多选和单选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23241019/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap