问题
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.
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;
}
];
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.
回答1:
Hope you won't mind, If I write some code for you. Don't just copy and paste it. Try to understand the way it is. So that you can tackle this kind of requirement in the future.
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"];
then the following tableView
methods.
- (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);
}
}
回答2:
Using the new Objective-C Literals syntax:
// NSInteger section, row;
NSDictionary *countryData = country[section];
NSMutableArray *cityData = countryData[@"citys"];
[cityData removeObjectAtIndex:row];
(note, for what it's worth: the English plural of city is cities.)
来源:https://stackoverflow.com/questions/16043480/nsmutabledictionary-as-datasource-can%c2%b4t-remove-cell