UITableView установлен на статические ячейки.Можно ли скрыть некоторые ячейки программно? - PullRequest
126 голосов
/ 24 ноября 2011

UITableView установлен на статические ячейки.

Можно ли программно скрыть некоторые ячейки?

Ответы [ 22 ]

0 голосов
/ 28 октября 2014

Решение от k06a (https://github.com/k06a/ABStaticTableViewController) лучше, потому что оно скрывает весь раздел, включая верхние и нижние колонтитулы ячеек, где это решение (https://github.com/peterpaulis/StaticDataTableViewController) скрывает все, кроме нижнего колонтитула.

EDIT

Я только что нашел решение, если вы хотите скрыть нижний колонтитул в StaticDataTableViewController. Это то, что вам нужно скопировать в файл StaticTableViewController.m:

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section] == 0) {
        return nil;
    } else {
        return [super tableView:tableView titleForFooterInSection:section];
    }
}

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

    CGFloat height = [super tableView:tableView heightForFooterInSection:section];

    if (self.originalTable == nil) {
        return height;
    }

    if (!self.hideSectionsWithHiddenRows) {
        return height;
    }

    OriginalSection * os = self.originalTable.sections[section];
    if ([os numberOfVissibleRows] == 0) {
       //return 0;
        return CGFLOAT_MIN;
    } else {
        return height;
    }

    //return 0;
    return CGFLOAT_MIN;
}
0 голосов
/ 08 мая 2015

Конечно, вы можете. Сначала верните в таблицу tableView количество ячеек, которые вы хотите отобразить, затем наберите super, чтобы получить определенную ячейку из раскадровки и вернуть ее для tableView:

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.mode.numberOfCells()
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = super.tableView(tableView, cellForRowAtIndexPath: self.mode.indexPathForIndexPath(indexPath))

    return cell
}

Если ваши клетки имеют различную высоту, верните также:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return super.tableView(tableView, heightForRowAtIndexPath: self.mode.indexPathForIndexPath(indexPath))
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...