Limited Selection in UITableView

105 Views Asked by At

i have a tableview getting dynamic data from a service. i want to select multiple rows limit is 3.. i have tried all the possible ways but can't get it out.. following is my code. Please help me out to solve this issue. Thanks in Advance

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.countriesArr count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];

    static NSString *MyIdentifier = @"tableCell";

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];


    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;

            }
        }

    }

    NSDictionary *thisRow = [self.countriesArr objectAtIndex:row];


    if(_WSConstCountryID !=nil && ![_WSConstCountryID isEqual:@"0"] && ![_WSConstCountryID isEqual:@""] && _WSConstCountrySelectedIndex ==row  )
    {
        cell .accessoryType=UITableViewCellAccessoryCheckmark;
    }
    else {
        cell .accessoryType=UITableViewCellAccessoryNone;
    }

    cell.lblTitle.text = [thisRow objectForKey:_WSColumnCountryName];
    NSString *str=[thisRow objectForKey:_WSColumnCountryID];
    NSString *stra=_WSConstCountryID;
    if ([str isEqualToString:stra]) {
        cell .accessoryType=UITableViewCellAccessoryCheckmark;
        cell.highlighted=YES;
    }else
    {
        cell .accessoryType=UITableViewCellAccessoryNone;

    }


    return cell;


}
#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSUInteger row = [indexPath row];
    NSDictionary *thisRow=[self.countriesArr  objectAtIndex:row];
    NSLog(@"%@",[thisRow description]);


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];



    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    if(_WSConstCountryID!=nil && ![_WSConstCountryID isEqual:@"0"] && ![_WSConstCountryID isEqual:@""] &&_WSConstCountrySelectedIndex!=row)
    {

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstCountrySelectedIndex inSection:0]];


        if (cell != nil)
        {
            cell .accessoryType=UITableViewCellAccessoryNone;
        }

    }
    if( cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        _WSConstCountryID=[thisRow objectForKey:_WSColumnCountryID];
        _WSConstCountryName=[thisRow objectForKey:_WSColumnCountryName];
        _WSConstCountrySelectedIndex=row ;
    }
    else
    {
        _WSConstCountryID=@"0";
        _WSConstCountryName=@"Select";
        _WSConstCountrySelectedIndex=-1 ;
    }

    [self.navigationController popViewControllerAnimated:YES];

}
2

There are 2 best solutions below

0
Kalaivani On

There are two things you have to follow for this. 1.Since you want to select maximum of only three rows simultaneously,keep an array for tracking those row numbers and check whether that array has the row number in CellForRowAtIndexPath.But _WSConstCountrySelectedIndex has only the latest selected row number.

2.In didSelectRowAtIndex,update your array with the current selected row number.And make sure array does not exceed the count of 3.Similarly in didDeSelectRowAtIndex when the selected row is deselected then remove that row number in the array.

It must give you a start.

0
Arasuvel On

In willSelectRowAtIndexPath check the count of number of rows selected. If it is less than 3 than return index path or else return nil.

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView indexPathsForSelectedRows].count > 3) {
        return nil;
    }
    return indexPath;
}

Hope this will help you.