[英]Delete Multiple Rows from UITableView
I currently have 2 methods to delete rows from a UITableView, the first one is the standard slide then press delete, the second deletes all rows that are checked. 我目前有2种方法可以从UITableView删除行,第一种是标准幻灯片,然后按Delete键,第二种删除所有已检查的行。 The only problem is when you select more then 1 or 2 it crashes with the following in the NSLog
唯一的问题是,当您选择大于1或2的值时,它会因NSLog中的以下内容而崩溃
2014-04-08 22:37:53.985 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000038016> {length = 2, path
= 0 - 7}
2014-04-08 22:37:53.987 PunchSlip[13128:60b] 7
2014-04-08 22:37:53.990 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000030016> {length = 2, path = 0 - 6}
2014-04-08 22:37:53.991 PunchSlip[13128:60b] 6
2014-04-08 22:37:53.993 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000028016> {length = 2, path = 0 - 5}
2014-04-08 22:37:53.995 PunchSlip[13128:60b] 5
2014-04-08 22:37:53.997 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000018016> {length = 2, path = 0 - 3}
2014-04-08 22:37:53.999 PunchSlip[13128:60b] 3
2014-04-08 22:37:54.001 PunchSlip[13128:60b] <NSIndexPath: 0xc000000000020016> {length = 2, path = 0 - 4}
2014-04-08 22:37:54.003 PunchSlip[13128:60b] 4
2014-04-08 22:37:54.005 PunchSlip[13128:60b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFArray removeObjectAtIndex:]: index (4) beyond bounds (4)'
My first delete method looks like this: 我的第一个删除方法如下所示:
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//delete row from array and from plist
[arrayFromPlist removeObjectAtIndex:indexPath.row];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"break.plist"];
[arrayFromPlist writeToFile:plistLocation atomically:YES];
[self.tableeView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
//[self.tableeView reloadData];
}
}
My Second Method, which i based off off the first looks like this : 我基于第二种方法的第二种方法如下所示:
-(IBAction)erase:(id)sender{
NSArray *selectedCells = [self.tableeView indexPathsForSelectedRows];
for (NSString *string in selectedCells) {
NSLog(@"%@", string);
NSString *stringOne = [NSString stringWithFormat:@"%@", string];
NSString *thisOne = [[stringOne componentsSeparatedByString:@" "] objectAtIndex:9];
NSString *thatOne = [[thisOne componentsSeparatedByString:@"}"] objectAtIndex:0];
int rowInt = [thatOne intValue];
NSLog(@"%d", rowInt);
[arrayFromPlist removeObjectAtIndex:rowInt];
// [tableeView deleteRowsAtIndexPaths:[NSArray arrayWithObject:string] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableeView reloadData];
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistLocation = [documentsDirectory stringByAppendingPathComponent:@"break.plist"];
[arrayFromPlist writeToFile:plistLocation atomically:YES];
[self viewDidLoad];
}
The second method knows which cells are selected as you can see in the log, but when you do "too many" in this case 4 it says there is an NSRangeExeption. 第二种方法知道您在日志中看到的是选中了哪些单元格,但是在这种情况下“太多”时4会显示存在NSRangeExeption。
Any Ideas on how to correct it? 关于如何纠正它的任何想法?
Try this for second method, 尝试第二种方法,
-(IBAction)erase:(id)sender{
NSArray *selectedCells = [self.tableeView indexPathsForSelectedRows];
NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
for (NSIndexPath *indexPath in selectedCells) {
[indicesToDelete addIndex:indexPath.row];
}
//arrayFromPlist is NSMutableArray
[arrayFromPlist removeObjectsAtIndexes:indicesToDelete];
[tableeView beginUpdates];
[tableeView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
[tableeView endUpdates];
....
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.