我想删除我的 2 个 tableViews 之一中的一个单元格(1 个是主要的,2 个是收藏夹)。
要删除一个单元格,我应该将特定值设置为“NO”,以便在 plist 中为 NO(收藏夹表仅显示“isFav”值设置为 YES 的单元格)。
检查此问题以获取更多详细信息:UITableView not shows the right cells
回到我的问题,我试着做
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *plist = [self readPlist];
NSMutableDictionary *theItem = [[[plist objectAtIndex:indexPath.section] valueForKey:@"Rows"] objectAtIndex:indexPath.row];
NSLog(@"%@",theItem);
if (editingStyle == UITableViewCellEditingStyleDelete) {
[theItem setValue:[NSNumber numberWithBool:NO] forKey:@"isFav"];
[self.tableView deleteRowsAtIndexPaths:[[[NSArray arrayWithObject:[plist objectAtIndex:indexPath.section]]valueForKey:@"Rows"]objectAtIndex:indexPath.row] withRowAnimation:UITableViewRowAnimationNone];
[self writePlist:plist];
[self.tableView reloadData];
}
}
- (void)writePlist:(NSArray*)arr
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"];
NSFileManager *fMgr = [NSFileManager defaultManager];
if ([fMgr fileExistsAtPath:plistPath])
[fMgr removeItemAtPath:plistPath error:nil];
[arr writeToFile:plistPath atomically:YES];
}
- (NSArray*)readPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"];
NSFileManager *fMgr = [NSFileManager defaultManager];
if (![fMgr fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"tipsList" ofType:@"plist"];
}
NSMutableArray *returnArr = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"isFav == YES"];
for (NSDictionary *sect in returnArr) {
NSArray *arr = [sect objectForKey:@"Rows"];
[sect setValue:[arr filteredArrayUsingPredicate:predicate] forKey:@"Rows"];
[self.tableView reloadData];
}
return returnArr;
}
但没有成功。我想做什么:尝试获取表中的当前项目,然后将其“isFav”值设置为 NO,然后从表中删除单元格,但我失败了,得到
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary row]: unrecognized selector sent to instance 0x7fd4ab0'
我试着做,[NSArray arrayWithObject:indexPath]
但我明白了
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (1), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
任何帮助表示赞赏:|