gpt4 book ai didi

ios - 如何限制 UITableViewController 中的多选

转载 作者:行者123 更新时间:2023-12-01 17:39:41 25 4
gpt4 key购买 nike

这里是tableView数据源和delegate的实现,我设置的最大选择是5。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
if ([multiOptions count] > 5) {
[self tableView:tableView didDeselectRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
//show alert
}
...
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[multiOptions removeObject:selectedOption];
...
}

但是这里有一个问题,如果选项超过限制,第一次单击单元格就可以了。但第二次点击单元格会调用
didDeselectRowAtIndexPath

再次,这不是我所期望的,我试过了
[tableView deselectRowAtIndexPath:indexPath animated:YES];

在 didSelectRowAtIndexPath 中,它不起作用,有人可以给我一个提示,如何纠正它?谢谢。

最佳答案

您必须在 tableView:willSelectRowAtIndexPath: 中进行检查

看看UITableView class reference
尤其是最后一句话:

Return Value An index-path object that confirms or alters the selected row. Return an NSIndexPath object other than indexPath if you want another cell to be selected. Return nil if you don't want the row selected.



所以这样的事情将在 tableView:willSelectRowAtIndexPath: 中工作。 :
if ([multiOptions count] > 5) return nil;

关于ios - 如何限制 UITableViewController 中的多选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24381005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com