![](/img/trans.png)
[英]Delete row from UITableView when I have multiple sections iOS (values are in Dynamic NSMutableDictionary)
[英]How do you edit(i.e. delete row) uiTableView when using sections?
您好,谢谢大家。 我有一个表格视图,允许用户更改为剖视图。 在标准视图中一切都很好,但是当用户切换到剖视图时,当我尝试使用(-commitEditingStyle :)函数删除一行时,我崩溃了。 我在此功能中缺少什么? 分段时应如何删除行? (我从plist加载我的Table,这是Arrays的字典。每个索引都是tableview中的一行。)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([strTitle isEqualToString:@"sectionView"]) {
@try {
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
// NSMutableDictionary *book =[[NSMutableDictionary alloc]init];
NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
NSMutableArray *sectionRow = [mynewList objectForKey:@"Dis"];
[sectionRow removeObjectAtIndex:indexPath.row];
[mynewList writeToFile:[self dataFilePath] atomically:YES];
//
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
@catch (NSException *exception) {
NSLog(@"hello error...%@",exception);
}
}
else {
/////////////////////// STANDARD VIEW
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source.
[distribArray removeObjectAtIndex:indexPath.row];
//
// NSMutableArray *contents = [[NSMutableArray alloc] init];
// [contents addObject:myMasterList];
// //[contents addObject:[NSArray arrayWithObjects:arraydata.text,distroName.text,destorEmail.text, nil]];
[distribArray writeToFile:[self dataFilePath] atomically:YES];
//
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
//////////////////////////////////
}
}
控制台:-[__ NSCFString removeObjectAtIndex:]:无法识别的选择器已发送到实例0xe4bd3f0
如您所见,我一直在尝试格式....不确定下一步该怎么做??? 谢谢。
我的意思是分段的:
因此,您有:
NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
您在其中分配mynewList,然后立即覆盖它。
但是我认为您可能应该检查调试器中sectionRow的类型。 我的猜测是它是一个NSString。
在不了解您的数据结构的情况下,很难说出正在发生的事情。
这里有两个问题。 下一行将产生泄漏,因此不应以这种方式使用。
NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
除了这个,你可以这样写,
NSMutableDictionary *mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
另一件事是
[mynewList objectForKey:@"Dis"];
并不总是一个数组,它有时以字符串形式出现。 检查您的数据结构,并确认它是否为数组。
这样尝试
id obj = [mynewList objectForKey:@"Dis"];
NSLog(@"%@", [[obj class] description]);
并检查在NSLog
打印的obj
的数据类型是什么。 确认它是NSMutableArray
而不是NSString
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.