gpt4 book ai didi

ios - 如何限制在uitableview中选择的行数

转载 作者:行者123 更新时间:2023-11-29 02:04:28 32 4
gpt4 key购买 nike

我正在创建一个 tableView,我想将选择的行数限制为 5 行。如果选择第 6 行,则会出现一条警报,指出我只能选择 5 行。我可以选中和取消选中行,但应选择最少 1 行和最多 5 行。我该怎么做。我当前的代码如下:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
countId = countId + 1;

}

else
{
cell.accessoryType = UITableViewCellAccessoryNone;
countId--;
}
// countId = [self.tableView indexPathsForSelectedRows].count;

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

我应该在哪里设置限制以及如何使用它?我能够在两个 View Controller 之间传递 countId 的值,我只是想知道如何将 countId 限制在 1 到 5 之间。我对此很陌生,因此我们将不胜感激。

最佳答案

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];



if (cell.accessoryType == UITableViewCellAccessoryNone)
{

if(countId <5)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
countId = countId + 1;
}
else
{
//show alert
NSLog(@"Greater then 5");
}


}

else
{
if(countId>1)
{
cell.accessoryType = UITableViewCellAccessoryNone;
countId--;
}
else
{
//show alert
NSLog(@"must choose 1");
}

}
// countId = [self.tableView indexPathsForSelectedRows].count;

[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

它可能对你有帮助.., :)

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

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