UITableView Пользовательский Заголовок Раздела, копируйте проблему

Я испытываю затруднения при анимации пользовательского заголовка раздела UITableView.
Цель состояла в том, чтобы создать разборные разделы.
Когда я касаюсь на пользовательском заголовке в первый раз, когда он анимирует как ожидалось, однако каждый раз после этого, он оставляет дубликат в исходном местоположении и анимирует другого.

Пример изображения:

alt text alt text
Мой пользовательский заголовок:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
       UIView* customView = [[[UIView alloc]initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)]autorelease];
       customView.backgroundColor = [UIColor lightGrayColor];

       UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
            headerLabel.backgroundColor = [UIColor clearColor];
            headerLabel.opaque = NO;
            headerLabel.textColor = [UIColor darkGrayColor];
            headerLabel.font = [UIFont boldSystemFontOfSize:16];
            headerLabel.frame = CGRectMake(10, 7, 260.0, 44.0);
            headerLabel.textAlignment = UITextAlignmentCenter;
            NSDictionary *dictionary = [self.data objectAtIndex:section];
            headerLabel.text = [dictionary objectForKey:@"Title"];

            [customView addSubview:headerLabel];


            // add button to right corner of section
        UIButton* headerButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
            headerButton.center = CGPointMake( 160.0, 22.0);
            headerButton.backgroundColor = [UIColor clearColor];
            headerButton.tag = section;
            [headerButton   addTarget:self action:@selector(expandSection:) forControlEvents:UIControlEventTouchUpInside];

            [customView addSubview:headerButton];

            return customView;
}

Мой метод анимации:

- (void) expandSection:(id)sender {

    if (expandedSection == [sender tag]) {
        expandedSection = -1;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else if (expandedSection == -1){
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView beginUpdates];  
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates]; 

    }
    //[self.tableView reloadData];
}

Я не абсолютно уверен, что продолжается, но экземпляры предлагают, чтобы мне было нужно к dealoc что-то. Я попробовал несколько вещей, но я не могу понять это. Любой помогает на этом, было бы большим!

Править: Я полагаю, что проблема состоит в том, что reloadSections вызывает пользовательское представление к экземпляру. Я не могу выпустить представление, потому что мне нужно оно как ссылка, чтобы сделать обновление для анимации. Какие-либо идеи о том, что я могу сделать для фиксации этого?

6
задан Glorfindel 23 June 2019 в 10:04
поделиться

1 ответ

Решение найдено.

Таблицу необходимо перезагружать перед каждым изменением. Таким образом, таблица находится в самом последнем состоянии до внесения каких-либо изменений.

добавьте [self.tableView reloadData]; в качестве первой записи в методе expandSection.

КОД:

- (void) expandSection:(id)sender {

  [self.tableView reloadData];

    if (expandedSection == [sender tag]) {
        expandedSection = -1;
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else if (expandedSection == -1){
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
    }else{
        [self.tableView beginUpdates];  
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:expandedSection] withRowAnimation:UITableViewRowAnimationNone];
        expandedSection = [sender tag];
        [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[sender tag]] withRowAnimation:UITableViewRowAnimationNone];
        [self.tableView endUpdates]; 

    }
    //[self.tableView reloadData];
}
8
ответ дан 10 December 2019 в 00:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: