karthikeyan
karthikeyan

Reputation: 3888

UITableview with multiple selection?

I am working on multiple selection in UITableviewCell,i can doing that,the problem occurs when selected value is added into array and remove it from array. I have taken selected value from another array.i think, the following scenario i did mistake "i have selected multiple row,then i am deselecting one by one its getting crashing" with following crash -[__NSArrayM removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0]'

I can understand issues but not able to solve,is there anyway to get selected values in array?

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    [selectedValue addObject:[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]];
    tableViewCell.accessoryView.hidden = NO;
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    [selectedValue removeObjectAtIndex:indexPath.row];
    //[selectedValue removeLastObject];
    tableViewCell.accessoryView.hidden = YES;
    tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}

Upvotes: 0

Views: 63

Answers (2)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

the error says [__NSArrayM removeObjectAtIndex:]: index 1 beyond bounds [0 .. 0]', you try to remove the index out of range.your array contains only one value but you try to remove second Index.

do like

create the one common array for comapre the checkmark value in CellforRowAtIndex

{

NSMutableArray *storeSelectedcell
}

- (void)viewDidLoad
{
    [super viewDidLoad];


    storeSelectedcell = [NSMutableArray array];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   // initilize the cell and additional code

    if ([storeSelectedcell containsObject:indexPath])
     cell.accessoryType = UITableViewCellAccessoryCheckmark;

    else
     cell.accessoryType = UITableViewCellAccessoryNone;


    return cell;
}


 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove the selection
    [tableView deselectRowAtIndexPath:indexPath animated:YES];



    // allow multiple selection
    if ([storeSelectedcell containsObject:indexPath])
      [storeSelectedcell removeObject:indexPath];
    else
      [storeSelectedcell addObject:indexPath];

    // finally refresh your table
    [tableView reloadData];
}

Upvotes: 1

karthikeyan
karthikeyan

Reputation: 3888

try this,have temporary reference in your delegate method and check

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *val =[NSString stringWithFormat:@"%@",[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]];
    [selectedValue addObject:val];
    tableViewCell.accessoryView.hidden = NO;
    tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *val =[NSString stringWithFormat:@"%@",[[listAns valueForKey:@"o_id"]objectAtIndex:indexPath.row]];
    [selectedValue removeObject:val];
    tableViewCell.accessoryView.hidden = YES;
    tableViewCell.accessoryType = UITableViewCellAccessoryNone;
}

Upvotes: 0

Related Questions