NSMutableDictionary as datasource: can´t remove cell

☆樱花仙子☆ 提交于 2019-12-24 19:52:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!