NSMutableDictionary 作为数据源:无法删除单元格

编程入门 行业动态 更新时间:2024-10-11 15:23:39
本文介绍了NSMutableDictionary 作为数据源:无法删除单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 NSMutableDictionary 来填充我的 UITableView,效果很好.但是,我想删除行,然后我必须在调用 -reloadData 方法之前从字典中删除目标单元格.

I use a NSMutableDictionary to populate my UITableView, which works just fine. However, I want to delete rows and then I have to remove the target cell from the Dictionary before I call the -reloadData method.

我很难找到 NSMutableDictionary 中的正确对象并将其删除.我将在下面发布结构.

I´m having a hard time getting to the right object in the NSMutableDictionary and delete it. I´ll post the structure below.

country = [ { citys = [ { id = 4; cityName = New York; }, { id = 5; cityName = LA; } ]; id = 2; countryName = US; }, { citys = [ { id = 21; cityName = Oslo; } ]; id = 31; countryName = Norway; }

];

我使用countryName"值作为标题部分的名称.所以,我知道已被点击的单元格,如何在数组中找到该对象并将其删除.

I use the "countryName" values as header sections name. So, I know the cell that has been tapped, how I can find that object in the array and remove it.

推荐答案

希望你不会介意,如果我为你写了一些代码.不要只是复制和粘贴它.尝试理解它的方式.以便您将来可以解决此类需求.

我在 plist 中有你的数据并将它提取到 NSMutableDictionary 然后像这样的 NSMutableArray.

I had your data in the plist and extracted it to the NSMutableDictionary and then NSMutableArray like this.

// In the viewDidLoad: method NSMutableDictionary *plistData = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"Countries" ofType:@"plist"]]; sectionedArray = [plistData objectForKey:@"Country"];

然后是下面的tableView方法.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return [sectionedArray count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. NSMutableArray *array = [[sectionedArray objectAtIndex:section] objectForKey:@"citys"]; return [array count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];; } // Configure the cell... NSMutableArray *array = [[sectionedArray objectAtIndex:indexPath.section] objectForKey:@"citys"]; NSDictionary *dict = [array objectAtIndex:indexPath.row]; cell.textLabel.text = [dict valueForKey:@"cityName"]; return cell; } // Override to support conditional editing of the table view. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } // Override to support editing the table view. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source NSMutableArray *array = [[sectionedArray objectAtIndex:indexPath.section] objectForKey:@"citys"]; [array removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; NSLog(@"Sectioned array %@", sectionedArray); } }

更多推荐

NSMutableDictionary 作为数据源:无法删除单元格

本文发布于:2023-06-03 23:42:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/484986.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据源   单元格   NSMutableDictionary

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!