我想在我的2个tableViews之一中删除一个单元格(1是主要的一个,2个是收藏夹)。
要删除单元格,我应该将特定值设置为“NO”,以便在plist中为NO(收藏夹表格仅显示“isFav”值设置为YES的单元格)。
请查看此问题以获取更多详细信息:UITableView not showing 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).'
任何帮助表示赞赏:|
答案 0 :(得分:1)
发生此错误是因为dataSource
计数与实际数据不同。删除项目时,tableView
单元格将会关闭,例如。从10到9.但是你没有删除dataSource
中的数据,所以最终会得到不同的count
,Xcode会给你一点点。
正如在聊天中广泛谈论的那样,我们用这段代码解决了问题:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tableView beginUpdates];
NSIndexPath *realIndex = [self realIndexPathForIndex:indexPath];
NSArray *plist = [self readFullPlist];
NSMutableDictionary *theItem = [[[plist objectAtIndex:realIndex.section] valueForKey:@"Rows"] objectAtIndex:realIndex.row];
[theItem setValue:[NSNumber numberWithBool:NO] forKey:@"isFav"];
[self writePlist:plist];
[self refreshTable];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
}
- (NSArray*)readFullPlist
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[documentPaths lastObject] stringByAppendingPathComponent:@"tipsList.plist"];
NSFileManager *fMgr = [NSFileManager defaultManager];
if (![fMgr fileExistsAtPath:plistPath]) {
NSString *bundlePlistPath = [[NSBundle mainBundle] pathForResource:@"tipsList" ofType:@"plist"];
[self writePlist:[NSArray arrayWithContentsOfFile:bundlePlistPath]];
}
return [NSArray arrayWithContentsOfFile:plistPath];
}
- (NSIndexPath*)realIndexPathForIndex:(NSIndexPath*)idxPath
{
NSArray *fullList = [self readFullPlist];
NSArray *subArr = [[fullList objectAtIndex:idxPath.section] objectForKey:@"Rows"];
int row = idxPath.row;
int newRow = 0;
for (NSDictionary *dic in subArr)
{
if ([[dic valueForKey:@"isFav"] boolValue]) {
if (row == 0) {
return [NSIndexPath indexPathForRow:newRow inSection:idxPath.section];
}
row--;
}
newRow++;
}
return idxPath;
}
答案 1 :(得分:0)
在tableView:commitEditingStyle:方法中,查找要删除tableView中行的语句。在这里你应该用NSIndexPaths移交一个数组。此行很难在您的代码段中阅读,因为它包含许多嵌套调用...