简体   繁体   English

当我有多个iOS部分时,从UITableView中删除行(值在Dynamic NSMutableDictionary中)

[英]Delete row from UITableView when I have multiple sections iOS (values are in Dynamic NSMutableDictionary)

Let me describe my scenario first. 首先让我描述一下我的情况。

I have a NSMutableDictionary called self.wordDic , which basically have some key & value like this: (This dictionary is dynamic, not fixed) 我有一个名为self.wordDicNSMutableDictionary ,它基本上具有一些keyvalue如下所示:(此字典是动态的,不是固定的)

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

Code: 码:

- (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:)];
    }
}

I have an NSArray called keyArray where I have all of my NSDictionary keys (Alphabetically) . 我有一个名为keyArrayNSArray ,其中有我所有的NSDictionary(按字母顺序) And then in cellForRowAtIndexPath I show all my values of a particular key (Alphabetically) . 然后在cellForRowAtIndexPath ,显示我所有特定key(按字母顺序)

Please take have a look into my tableView methods : 请看看我的tableView方法:

My 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;
}

Everything works perfectly. 一切正常。 But now I want to delete my rows. 但是现在我要删除行。

- (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];
    }
}

In commitEditingStyle with which array I should have to replace this self.whichArraywillBeHere array? commitEditingStyle中,我应该使用哪个数组替换此self.whichArraywillBeHere数组? Cause, first I have no section in this method and secondly, I load my rows, alphabetically. 原因是,首先,此方法中没有节,其次,按字母顺序加载行。 SO how can I know which array, should I used here and what will be it's indexPath ?? 所以我怎么知道该在哪个数组上使用,它应该是indexPath If you understand my problem please reply. 如果您了解我的问题,请回复。

Thanks a lot in advance. 非常感谢。
Have a good day. 祝你有美好的一天。 :) :)

i think you're very close - you already have your keyArray sorted and stored in a property. 我认为您非常接近-您已经将keyArray排序并存储在属性中。 You can get the values array from the dictionary with this - just like you did in the data source methods. 您可以使用此方法从字典中获取values数组,就像在数据源方法中所做的一样。 The quick and dirty way to delete the right row/word would be to just sort it again. 删除正确的行/单词的快速而肮脏的方法是再次对其进行排序。

-(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];
}

Again this is the quick solution, note that you sort the array(s) each time a row is deleted and also anytime the table is reloaded (but if your word arrays are small this may not matter - that's up to you) 再次,这是一种快速的解决方案,请注意,每次删除一行以及每次重新加载表时都要对数组进行排序(但是,如果您的单词数组很小,则可能无关紧要,这取决于您)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM