NSMutableDictionary 作为数据源:无法删除单元格 [英] NSMutableDictionary as datasource: can´t remove cell

查看:63
本文介绍了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 作为数据源:无法删除单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆