0

I am creating a tableView where I want to limit number of rows selected to 5. If 6th row is selected then a alert will appear stating that I can select only 5 rows. I can check and uncheck the rows but minimum 1 row and maximum 5 rows should be selected. How can I do it. My current code is as follows:

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

Where should I set the limit and how to use it? I am able to pass the value of countId between two view controllers I just want to know how I can limit the countId to be between 1 and 5. I am new to this so any help will be appreciated.

Itaws
  • 330
  • 1
  • 22
  • Well, you'd have to write some code to actually check your count. – Hot Licks Apr 29 '15 at 11:24
  • I am getting the total count and passing it to another view controller. What I want to know is how can I limit the count to be between 1 and 5 – Itaws Apr 29 '15 at 11:34
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Apr 29 '15 at 11:35
  • So then it's really a "passing data between view controllers" question. – Hot Licks Apr 29 '15 at 11:36
  • I am able to pass data between view controllers. I just wanted to know how I can limit the countId to 5. Now the problem has been solved. – Itaws Apr 29 '15 at 11:45
  • I'm confused -- you didn't know how to write `if (countId < 5)`? – Hot Licks Apr 29 '15 at 11:49
  • The thing is I was confused where I should write the condition. If I should write it in didSelectRowAtIndexPath or at the next view where I am using the value. – Itaws Apr 29 '15 at 11:51

1 Answers1

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

It may be help ful to you.., :)

Mohanraj S K
  • 657
  • 4
  • 16