Как удалить разделы из статической таблицы ячеек - PullRequest
5 голосов
/ 15 декабря 2011

Я пытаюсь удалить или скрыть раздел в виде таблицы со статическими ячейками на нем.Я пытаюсь скрыть это в функции viewDidLoad.Вот код:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.tableView beginUpdates];
    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:YES];
    [self.tableView endUpdates];

    [self.tableView reloadData];
}

Тем не менее разделы появляются.Я использую раскадровки в нем.Можете ли вы помочь мне? Спасибо!

Ответы [ 4 ]

6 голосов
/ 23 августа 2012

Мне было наиболее удобно скрывать разделы путем переопределения numberOfRowsInSection.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ( section == 1 )
        // Hide this section
        return 0;
    else
        return [super tableView:self.tableView numberOfRowsInSection:section];
}
5 голосов
/ 03 февраля 2012

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

0 голосов
/ 18 февраля 2016

Вот, пожалуйста.Этот также удаляет вертикальное пространство.

NSInteger sectionToRemove = 1;
CGFloat distance = 10.0f; // minimum is 2 since 1 is minimum for header/footer
BOOL removeCondition; // set in viewDidLoad

/**
 *  You cannot remove sections.
 *  However, you can set the number of rows in a section to 0,
 *  this is the closest you can get.
 */

- (NSInteger)tableView:(UITableView *)tableView
 numberOfRowsInSection:(NSInteger)section {
  if (removeCondition && section == sectionToRemove)
    return 0;
  else
    return [super tableView:self.tableView numberOfRowsInSection:section];
}

/**
 *  In this case the headers and footers sum up to a longer
 *  vertical distance. We do not want that.
 *  Use this workaround to prevent this:
 */
- (CGFloat)tableView:(UITableView *)tableView
    heightForFooterInSection:(NSInteger)section {

  return removeCondition &&
                 (section == sectionToRemove - 1 || section == sectionToRemove)
             ? distance / 2
             : distance;
}

- (CGFloat)tableView:(UITableView *)tableView
    heightForHeaderInSection:(NSInteger)section {

  return removeCondition &&
                 (section == sectionToRemove || section == sectionToRemove + 1)
             ? distance / 2
             : distance;
}
0 голосов
/ 21 декабря 2011

Кажется, что reloadData делает табличное представление для перечитывания dataSource.Вам также следует удалить данные из источника данных перед вызовом reloadData.Если вы используете массив, удалите нужный объект с помощью removeObject: перед вызовом reloadData.

...