How to add a button with click event on UITableViewCell in Swift?

前端 未结 3 729
耶瑟儿~
耶瑟儿~ 2020-12-23 16:34

In my main page, I created a xib file for UITableViewCell. I\'m loading the cell from that xib file and its working fine.

Inside of the cell I have some labels and

3条回答
  •  旧巷少年郎
    2020-12-23 16:57

    All patterns above are fine. my two cents, in case You add by code (for example multiple different cells and so on..) there is a FAR simple solution.

    As buttons allow to specify a "target" You can pass directly the controller AND action to cell/button when setting it.

    In controller:

    let selector = #selector(self.myBtnAction)
    setupCellWith(target: self, selector: selector)
    

    ...

    in custom cell with button:

    final func setupCellWith(target: Any? selector: Selector){
           btn.addTarget(target, 
            action: selector,
           for: .touchUpInside)
    
    }
    

提交回复
热议问题