Objective c 通过UITableView从NSMutableDictionary添加/删除项
我有一个带有NSMutableDictionary的.plist文件,我正在填充UITableView,如下所示:Objective c 通过UITableView从NSMutableDictionary添加/删除项,objective-c,ios,xcode,cocoa-touch,Objective C,Ios,Xcode,Cocoa Touch,我有一个带有NSMutableDictionary的.plist文件,我正在填充UITableView,如下所示: - (void)viewDidLoad { [super viewDidLoad]; NSString *myFile = [[NSBundle mainBundle] pathForResource:@"courses" ofType:@"plist"]; courses = [[NSMutableDictionary alloc] in
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *myFile = [[NSBundle mainBundle]
pathForResource:@"courses" ofType:@"plist"];
courses = [[NSMutableDictionary alloc] initWithContentsOfFile:myFile];
courseKeys = [courses allKeys];
}
以及:
我试图允许用户使用UiTableView单元格滑动+删除并向plist“添加”新项目。这是我用来做这件事的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[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
当我运行模拟器并执行此操作时,它会给我一个错误,声明
无效更新:节0中的行数无效。更新后现有节中包含的行数(7)必须等于更新前该节中包含的行数(7),加上或减去从该节插入或删除的行数(0插入,1删除)加上或减去移入或移出该节的行数(0移入,0移出)。”
有人能帮我吗 您正在从courseKeys数组填充表。所以在委托方法中
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//whether it is an add or edit or delete modify your array from which you are populating the table and reload the table
}
您正在从courseKeys数组填充表。所以在委托方法中
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
//whether it is an add or edit or delete modify your array from which you are populating the table and reload the table
}
您正在从表中删除该行,而不是从数据源中删除该行。CourseKeys需要是一个可变数组,可以从中删除已删除的键 您正在从表中删除该行,而不是从数据源中删除该行。CourseKeys需要是一个可变数组,可以从中删除已删除的键 谢谢你。我真的很难弄明白怎么做--我把数组改成了nsmutablearray,现在它抛出一个错误,说它不对。在总数方面仍然存在相同的错误。有什么建议吗?谢谢你。我真的很难弄明白怎么做--我把数组改成了nsmutablearray,现在它抛出一个错误,说它不对。在总数方面仍然存在相同的错误。有什么建议吗?