Подтвердить что ты не робот

Как добавить нижний колонтитул в UITableView?

Я использую этот код, чтобы добавить нижний колонтитул в 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

4b9b3361

Ответ 1

Я вижу в своем коде код, который

self.theTable.tableFooterView = tableFooter;

работает и

[self.theTable.tableFooterView addSubview:tableFooter];

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

Ответ 2

Вам нужно реализовать метод UITableViewDelegate

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

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

Ответ 3

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

    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))];

Ответ 4

Я знаю, что это довольно старый вопрос, но я только что встретил такую ​​же проблему. Я не знаю точно, почему, но кажется, что 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];

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

Ответ 5

Сначала я просто пытался использовать метод:

- (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

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 есть пробел.

Ответ 7

[self.tableView setTableFooterView:footerView];

Ответ 8

вместо

self.theTable.tableFooterView = tableFooter;

попробуйте

[self.theTable.tableFooterView addSubview:tableFooter];

Ответ 9

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

@interface MyController : UIViewTableViewController <UITableViewDelegate, UITableViewDataSource>

с этой строкой и работает как ожидалось:

@interface RequestViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

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

Ответ 10

Эти образцы хорошо работают. Вы можете проверить раздел, а затем вернуть высоту, чтобы показать или скрыть раздел. Не забудьте расширить свой 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;
}

стриж

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

Ответ 11

Если вы не предпочитаете эффект липкого дна, я бы поставил его в viewDidLoad() fooobar.com/info/123716/...