Как добавить две кнопки UIB в заголовок раздела Tableview - PullRequest
0 голосов
/ 28 октября 2018

Я добавил настраиваемый заголовок таблицы с двумя кнопками, но кнопки отключены, не в состоянии создавать события управления.Я хочу получить макет, как это.Я новичок в разработке.любые предложения или решения i want selectAll/Cancel and Send buttons in Tableview section header

я пытался добавить представление с кнопками внутри представления в функции ViewforHeaderSection

func tableView (_ tableView: UITableView, раздел viewForHeaderInSection: Int)-> UIView?{

    let inviteSectionHeaderview  = UIView.init(frame: CGRect(x:0, y:0, width: self.view.bounds.width, height: self.view.bounds.height))
    let selectAllBtn = UIButton.init(frame:CGRect(x:16, y: inviteSectionHeaderview.bounds.height/2, width:130, height:20))
    let sendButton = UIButton.init(frame:CGRect(x:inviteSectionHeaderview.bounds.width - 30, y: inviteSectionHeaderview.bounds.height/2, width:60, height:20))
    selectAllBtn.setTitle("select all/Cancel", for: .normal)
    selectAllBtn.backgroundColor = .black
    sendButton.backgroundColor = .black
    sendButton.setTitle("SEND", for: .normal)
    self.contactsTable.addSubview(inviteSectionHeaderview)
    inviteSectionHeaderview.addSubview(selectAllBtn)
    inviteSectionHeaderview.addSubview(sendButton)
    return inviteSectionHeaderview

}

by implementing above code i'm getting like this

Ответы [ 2 ]

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

Вы можете попробовать этот код.

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        var headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerView")

        if headerView == nil {
            headerView = UITableViewHeaderFooterView(reuseIdentifier: "headerView")

            let button1 = UIButton(frame: CGRect(x: 8, y: 8, width: 80, height: 40))
            button1.setTitle("Select", for: .normal)
            button1.addTarget(self, action: #selector(self.buttonAction), for: .touchUpInside)
            headerView?.addSubview(button1)

        }

        return headerView
    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 50 // set a height for header view
    }

    @objc func buttonAction(_ sender: UIButton) {
     // write your code...
    }
0 голосов
/ 28 октября 2018

У вас есть два варианта:

  1. Создать свой UIView в раскадровке
  2. Создать программно

Опция 2

  1. Создайте UIView.

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? 
        let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 30.0))        
    
        // Button1
        let button1 = UIButton(frame: CGRect(x: 15.0, y: 0, width: 100, height: 28.0)
        button1.setTitle("Button 1", for: .normal)
        button1.addTarget(self, action: #selector(selectorButton1), for: .touchUpInside)
    
        // Button2
        let button2 = UIButton(frame: CGRect(x: tableView.frame.width-150, y: 0, width: 150, height: 30.0))
        button2.setTitle("Button2", for: .normal)
        button2.addTarget(self, action: #selector(selectorButton2), for: .touchUpInside)
        button2.semanticContentAttribute = UIApplication.shared
            .userInterfaceLayoutDirection == .rightToLeft ? .forceLeftToRight : .forceRightToLeft
    
        headerView.addSubview(button1)
        headerView.addSubview(button2)
        return headerView 
    }
    
    
        @objc func selectorButton1(_ sender : Any) {
    
        }
    
        @objc func selectorButton2(_ sender : Any) {
    
        }
    

В этом случае вы должны правильно установить y и x позиции при созданииUIView(frame: CGRect()) и UIButton(frame: CGRect())

РЕДАКТИРОВАТЬ

Из вашего кода вам просто нужно добавить цели:

selectAllBtn.addTarget(self, action: #selector(selectorAllBtn), for: .touchUpInside)
sendButton.addTarget(self, action: #selector(selectorSendButton), for: .touchUpInside)

@objc func selectorAllBtn(_ sender : Any) {

}

@objc func selectorSendButton(_ sender : Any) {

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...