Как добавить нижний колонтитул в UITableView? - PullRequest
53 голосов
/ 28 февраля 2011

Я использую этот код для добавления нижнего колонтитула в TableView. В нем 20 разделов, а в каждом разделе несколько рядов. Есть методы titleForHeaderInSection и sectionForSectionIndexTitle.

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

Что я делаю не так?

спасибо,

RL

Ответы [ 11 ]

36 голосов
/ 06 июня 2012

Вам необходимо реализовать метод UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

и верните желаемое представление (например, UILabel с текстом, который вы хотите в нижнем колонтитуле) для соответствующего раздела таблицы.

36 голосов
/ 31 мая 2011

Я специально вижу в своем коде, что

self.theTable.tableFooterView = tableFooter;

работает и

[self.theTable.tableFooterView addSubview:tableFooter];

не работает.Так что придерживайтесь первого и ищите в другом месте возможную ошибку.НТН

18 голосов
/ 28 июня 2012

Я использовал это, и это сработало отлично :)

    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 500)];
    [footerView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"ProductCellBackground.png"]]];
    self.tableView.tableFooterView = footerView;
    [self.tableView setSeparatorStyle:(UITableViewCellSeparatorStyleNone)];
    [self.tableView setContentInset:(UIEdgeInsetsMake(0, 0, -500, 0))];
6 голосов
/ 21 декабря 2014

Изначально я просто пробовал метод:

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

но после использования вместе с:

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

проблема была решена. Пример программы -

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *sampleView = [[UIView alloc] init];
    sampleView.frame = CGRectMake(SCREEN_WIDTH/2, 5, 60, 4);
    sampleView.backgroundColor = [UIColor blackColor];
    return sampleView;
}

и включают протокол UITableViewDelegate.

@interface TestViewController : UIViewController <UITableViewDelegate>
6 голосов
/ 20 сентября 2013

Я знаю, что это довольно старый вопрос, но я только что столкнулся с той же проблемой.Я не знаю точно, почему, но кажется, что tableFooterView может быть только экземпляром UIView (не «вид», а «является членом») ... Так что в моем случае я создал новый объект UIView (например, wrapperView)и добавьте к нему мое собственное подпредставление ... В вашем случае измените код с:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];

на:

CGRect footerRect = CGRectMake(0, 0, 320, 40);
UIView *wrapperView = [[UIView alloc] initWithFrame:footerRect];

UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = [UIColor blueColor];
tableFooter.backgroundColor = [self.theTable backgroundColor];
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = @"test";

[wrapperView addSubview:tableFooter];

self.theTable.tableFooterView = wrapperView;
[wrapperView release];
[tableFooter release];

Надеюсь, это поможет.У меня это работает.

4 голосов
/ 14 февраля 2016

Swift 2.1.1 ниже работает:

func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        let v = UIView()
        v.backgroundColor = UIColor.RGB(53, 60, 62)
        return v
    }

    func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 80
    }

При использовании self.theTable.tableFooterView = tableFooter между последней строкой и tableFooterView.

есть пробел.
3 голосов
/ 21 марта 2018

Эти образцы работают хорошо. Вы можете проверить раздел, а затем вернуть высоту, чтобы показать или скрыть раздел. Не забудьте расширить ваш viewcontroller с UITableViewDelegate.

Objective-C

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0)
    {
        // to hide footer for section 0 
        return 0.0;
    }
    else
    {
        // show footer for every section except section 0 
        return HEIGHT_YOU_WANT;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc] init];
    footerView.backgroundColor = [UIColor blackColor];
    return footerView;
}

Swift

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let footerView = UIView()
    footerView.backgroundColor = UIColor.black
    return footerView
}

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    if section == 0 {
        // to hide footer for section 0
        return 0.0
    } else {
        // show footer for every section except section 0
        return HEIGHT_YOU_WANT
    }
}
2 голосов
/ 12 апреля 2017
[self.tableView setTableFooterView:footerView];
1 голос
/ 04 сентября 2011

У меня была такая же проблема, но я заменил следующую строку в заголовке:

@interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource>

с этой строкой, и она работает как положено:

@interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

Обратите внимание на UIViewController. Удачи:)

1 голос
/ 28 февраля 2011

вместо

self.theTable.tableFooterView = tableFooter;

попробуй

[self.theTable.tableFooterView addSubview:tableFooter];
...