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

时间:2013-04-16 17:36:22

标签: ios objective-c nsmutabledictionary

我使用NSMutableDictionary来填充我的UITableView,它运行得很好。但是,我想删除行,然后我必须在调用-reloadData方法之前从Dictionary中删除目标单元格。

我很难找到NSMutableDictionary中的正确对象并删除它。我将发布以下结构。

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”值作为标题部分名称。所以,我知道已被挖掘的单元格,我如何在数组中找到该对象并将其删除。

2 个答案:

答案 0 :(得分:1)

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

我将您的数据放在plist中,然后将其提取到NSMutableDictionary,然后将其提取到NSMutableArray

// 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);
    }   
}

答案 1 :(得分:0)

使用新的Objective-C Literals语法:

// NSInteger section, row;
NSDictionary *countryData = country[section];
NSMutableArray *cityData = countryData[@"citys"];
[cityData removeObjectAtIndex:row];

(注意,它的价值是什么: city 的英文复数是 cities 。)

相关问题