Reputation: 61
on the first screen user enters data & displaying it in second ViewController using prepareforsegue method.on the second screen user selects multiple rows for delete then again user selects remaining rows for delete application crash.Here is my code
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return mAryValue.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * strIdent=@"id1Cell";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:strIdent];
if (cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strIdent];
}
cell.textLabel.text=[mAryValue objectAtIndex:indexPath.row];
return cell;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.editing)
{
return;
}
[_tblView deselectRowAtIndexPath:[_tblView indexPathForSelectedRow] animated:NO];
UITableViewCell *cell = [_tblView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[mArySel addObject:indexPath];
}
else if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[mArySel removeObjectIdenticalTo:indexPath];
}
}
-(void)getData:(NSMutableArray *)userValues
{
mAryValue=userValues;
}
- (IBAction)btnDelete:(id)sender
{
if (mAryValue==nil || mAryValue.count==0)
{
UIAlertController * alert= [UIAlertController
alertControllerWithTitle:@"Alert"
message:@"Please Enter Value"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:@"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[alert dismissViewControllerAnimated:YES completion:nil];
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
}
else
{
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in mArySel)
{
[indicesToDelete addIndex:indexPath.row];
}
if (!(indicesToDelete==nil) || !(indicesToDelete.count==0))
{
[mAryValue removeObjectsAtIndexes:indicesToDelete];
}
[_tblView reloadData];
}
}
Please suggest me. Thank you.
Upvotes: 1
Views: 115
Reputation:
[NSMutableArray removeObjectsAtIndexes:]: index 5 in index set beyond bounds [0 .. 4]'
You are trying to remove an object at an index on your array
that does not exist. As the error says, you are removing objectAtIndex:5
, but your array only has 4 items.
[mAryValue removeObjectsAtIndexes:indicesToDelete];
Upvotes: 1