Добавить кнопку в нижнем колонтитуле footview

У меня есть tableview в одном viewcontroller. У меня есть один раздел. Я хочу добавить кнопку в нижнем колонтитуле. Я написал этот код, но нижний колонтитул не отображается.

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

    UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];
    [addcharity setTitle:@"Add to other" forState:UIControlStateNormal];
    [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];
    addcharity.frame=CGRectMake(0, 0, 40, 30);
    [footerView addSubview:addcharity];
    return footerView;

}

Я также установил высоту нижнего колонтитула 100 в свой метод делегирования.

скриншоты: Первоначально у меня есть 3 данных. Но когда я ищу конкретные данные, и результат будет отображаться в представлении таблицы, в это время у меня есть одна информация, поэтому в это время изменилось место нижнего колонтитула. Я хочу установить нижний колонтитул на начальном месте. enter image description here

enter image description here

Edit:

В качестве альтернативного решения можно добавить кнопку, добавив дополнительную ячейку в конец.

Ответы

Ответ 1

Per Apple Docs вы должны также реализовать метод heightForFooterInSection, иначе ваш viewForFooterInSection wouldn ' ничего не делайте.

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

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if(tableView == myListTableview)  //Here you can make decision 
    {
       UIView *footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
       UIButton *addcharity=[UIButton buttonWithType:UIButtonTypeCustom];
       [addcharity setTitle:@"Add to other" forState:UIControlStateNormal];
       [addcharity addTarget:self action:@selector(addCharity:) forControlEvents:UIControlEventTouchUpInside];
       [addcharity setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  //Set the color this is may be different for iOS 7
       addcharity.frame=CGRectMake(0, 0, 130, 30); //Set some large width for your title 
       [footerView addSubview:addcharity];
       return footerView;
    }
}

- (void)addCharity:(id)sender
{
      NSLog(@"add to charity");
}

Ответ 2

Тот же код в быстрой

func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 100.0
    }
    func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {

        let footerView = UIView(frame:CGRectMake(0,0,320,40))

        let loginButton = UIButton(type: .Custom)
        loginButton.setTitle("LOGIN", forState: .Normal)
        loginButton.addTarget(self, action: "loginAction", forControlEvents: .TouchUpInside)
        loginButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        loginButton.backgroundColor = UIColor.blueColor()
        loginButton.frame = CGRectMake(0, 0, 130, 30)

        footerView.addSubview(loginButton)

        return footerView
    }

    func loginAction()
    {
        print("Hello");
    }

Ответ 3

Будьте уверены в трех вещах

1- Вы не реализуете этот метод

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

2- Этот метод

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

Является ли метод UITableViewDelegate, а не UITableViewDataSource, проверьте, установлен ли делегат таблицы view с контроллером.

3- Убедитесь, что вы реализуете этот метод

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

Наконец. Если все правильно и все еще не работает, вы можете просто добавить представление в свойство tableFooterView таблицы как нижний колонтитул всей таблицы, а не только раздел