【问题标题】:Delete row from UITableView when I have multiple sections iOS (values are in Dynamic NSMutableDictionary)当我有多个 iOS 部分时,从 UITableView 中删除行(值在 Dynamic NSMutableDictionary 中)
【发布时间】:2016-01-24 21:50:49
【问题描述】:

让我先描述一下我的场景。

我有一个NSMutableDictionaryself.wordDic,基本上有一些key & value 是这样的:(这个字典是动态的,不是固定的)

key     value
---------------------------------------------------------
A       (Apple, Aim, Arise, Attempt, Airplane, Absolute)
B       (Bubble, Bite, Borrow, Basket)
C       (Cat, Correct)
D       (Dog, Direction, Distribute)

代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if ([self.wordListArray count] != 0)
    {
        self.wordDic = [self sortedDictionary:self.wordInWordListArray];

        // Sorted key array
        self.keyArray = [[NSArray alloc] init];
        NSArray *key = [[NSArray alloc] init];
        key = [self.wordDic allKeys];
        self.keyArray = [key sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    }
}

我有一个名为keyArrayNSArray,其中有我所有的NSDictionary(按字母顺序)。然后在cellForRowAtIndexPath 中,我显示了我对特定key 的所有值(按字母顺序)

请查看我的tableView 方法:

我的tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [self.keyArray count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 28;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
    // Add title label
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 100, 18)];
    [titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0f]];
    NSString *string = [self.keyArray objectAtIndex:section];

    [titleLabel setText:string];
    [view addSubview:titleLabel];

    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHomeTableViewCellHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSString *key = [self.keyArray objectAtIndex:section];
    NSMutableArray *value = [self.wordDic objectForKey:key];

    return [value count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = kHomeTableViewCellID;
    HomeTableViewCell *cell = (HomeTableViewCell *)[self.homeTableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell)
    {
        cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    NSString *secTitle = [self.keyArray objectAtIndex:indexPath.section];
    secData = [self.wordDic objectForKey:secTitle];
    [secData sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSString *data = [secData objectAtIndex:indexPath.row];
    [cell.wordLabel setText:data];

    return cell;
}

一切都很完美。但现在我想删除我的行。

- (void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Other code
    ..............
    ..............


    // remove info from tableView array
    [self.whichArraywillBeHere removeObjectAtIndex:indexPath.row];
    [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

commitEditingStyle 中,我应该用哪个数组替换这个 self.whichArraywillBeHere 数组?原因,首先我在这个方法中没有部分,其次,我按字母顺序加载我的行。那么我怎么知道我应该在这里使用哪个数组以及indexPath 是什么? 如果您理解我的问题,请回复。

非常感谢。
祝你有美好的一天。 :)

【问题讨论】:

    标签: ios objective-c uitableview nsdictionary nsmutabledictionary


    【解决方案1】:

    我认为您非常接近 - 您已经将您的 keyArray 排序并存储在一个属性中。您可以使用这个从字典中获取值数组 - 就像您在数据源方法中所做的那样。删除正确的行/单词的快速而肮脏的方法是再次排序。

    -(void)tableView:(UITableView *) tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // Other code
      // ..
      // remove info from tableView array
    
      NSString *key = [self.keyArray objectAtIndex:indexPath.section];
      NSMutableArray *words = [self.wordDic objectForKey:key];
      [words sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
      [words removeObjectAtIndex:indexPath.row];
      [self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    

    这也是快速解决方案,请注意,每次删除一行以及重新加载表时,您都要对数组进行排序(但如果您的单词数组很小,这可能无关紧要 - 这取决于您)

    【讨论】:

    • 谢谢。我不知道为什么昨晚这个简单的事情没有出现在我的脑海中。
    猜你喜欢
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    相关资源
    最近更新 更多