[英]How do you edit(i.e. delete row) uiTableView when using sections?
Hello and Thanks to all. 您好,谢谢大家。 I have a table view where I allow the user to change to a sectioned view.
我有一个表格视图,允许用户更改为剖视图。 All is well in standard view however when the user switches to sectioned view I crash when I try to delete a row with the (-commitEditingStyle:) function.
在标准视图中一切都很好,但是当用户切换到剖视图时,当我尝试使用(-commitEditingStyle :)函数删除一行时,我崩溃了。 What am I missing in this function?
我在此功能中缺少什么? How should I delete a row when It's sectioned?
分段时应如何删除行? (I load my Table from a plist wchich is a dictionary of Arrays. Each index being a row in the tableview.)
(我从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.
}
//////////////////////////////////
}
}
CONSOLE: -[__NSCFString removeObjectAtIndex:]: unrecognized selector sent to instance 0xe4bd3f0 控制台:-[__ NSCFString removeObjectAtIndex:]:无法识别的选择器已发送到实例0xe4bd3f0
As you can see I have been toying with the format....not sure what to try next ??? 如您所见,我一直在尝试格式....不确定下一步该怎么做??? Thanks.
谢谢。
what I mean by sectioned: 我的意思是分段的:
So for one, you've got: 因此,您有:
NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
where you are allocing mynewList and then immediately overwriting it. 您在其中分配mynewList,然后立即覆盖它。
But I think you should probably check the type of sectionRow in the debugger. 但是我认为您可能应该检查调试器中sectionRow的类型。 My guess is that it's an NSString.
我的猜测是它是一个NSString。
It's pretty hard to tell what's going on without knowing a bit more about your data structures. 在不了解您的数据结构的情况下,很难说出正在发生的事情。
Two problems are here. 这里有两个问题。 The below line will create a leak and should not be used in this way.
下一行将产生泄漏,因此不应以这种方式使用。
NSMutableDictionary *mynewList = [[NSMutableDictionary alloc]init];
mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
Instead of this you can put it as, 除了这个,你可以这样写,
NSMutableDictionary *mynewList = [[self.TitleDis valueForKey:[[[self.TitleDis allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
Another thing is, 另一件事是
[mynewList objectForKey:@"Dis"];
is not always an array and it is sometimes coming as string. 并不总是一个数组,它有时以字符串形式出现。 Check your data structure and confirm if it is an array.
检查您的数据结构,并确认它是否为数组。
Try like this, 这样尝试
id obj = [mynewList objectForKey:@"Dis"];
NSLog(@"%@", [[obj class] description]);
and check what is the data type of obj
printed in NSLog
. 并检查在
NSLog
打印的obj
的数据类型是什么。 Confirm it is NSMutableArray
and not NSString
. 确认它是
NSMutableArray
而不是NSString
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.