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

时间:2015-04-29 11:20:39

标签: ios objective-c uitableview xcode6.3

我正在创建一个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];
}

我应该在哪里设置限制以及如何使用它?我能够在两个视图控制器之间传递countId的值我只想知道如何将countId限制在1到5之间。我是新手,所以任何帮助都将受到赞赏。

1 个答案:

答案 0 :(得分:1)

-(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];
}

对你来说可能有帮助..,:)

相关问题