1

I'm making a custom cell from XIB file and implementing in my tableView, I am able to implement the text from the custom cell but how can I implement the same for the button and receive touches on it ?

Here is the code I wrote:

struct cellData {

let cell : Int!
let text : String!
let button =  UIButton()
}

var arrayOfData = [cellData]()

override func viewDidLoad() {
    super.viewDidLoad()

arrayOfData = [cellData(cell : 1, text: "ahfhasdf", button: button)]
 }

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if arrayOfData[indexPath.row].cell==1{

        let cell=Bundle.main.loadNibNamed("CustomTableViewCell1", owner: self, options: nil)?.first as! CustomTableViewCell1

        cell.label1.text=arrayOfData[indexPath.row].text
        cell.pillImage.currentImage=arrayOfData[indexPath].pillImage

        return cell
}

My pill is the button in CustomTableViewCell1 which I want in my cell and how to have actions when the button is pressed ? Please help.

  • take ref : -> https://stackoverflow.com/questions/28894765/ios-swift-button-action-in-table-view-cell – dahiya_boy Jun 02 '17 at 11:11
  • There are so many problem in this piece of code i'm not sure where to start...I suggest you to find some tutorial to read first, maybe [this](https://stackoverflow.com/questions/20655060/get-button-click-inside-ui-table-view-cell), other problems is your naming, holding UI object in struct, arrayOfData have image but your struct dont, create cell by `loadNibName` .... – Tj3n Jun 02 '17 at 11:13
  • @Tj3n yes I was not sure how to pass UIButton but I knew about UIImage thats why I wrote for UIImage and it works, but I want for a UIButton – Raunak Trikha Jun 02 '17 at 11:15

1 Answers1

0

This is way off from what you are asking in your question but I believe you are looking for something like this. I hope the code and comments speak for themselves.

// My cell from xib (storyboard)
class MyTableViewCell: UITableViewCell {

    @IBOutlet weak var button: UIButton!
    @IBOutlet weak var cellImageView: UIImageView!
    @IBOutlet weak var label: UILabel!

}

// My view controlle from xib (storyboard)
class MyViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!

    fileprivate var dataSource: [CellData] = [CellData]()

    override func viewDidLoad() {
        super.viewDidLoad()

        dataSource = [CellData(index: 0, title: "First cell", image: nil, target: self, selector: #selector(firstCellPressed)),
                      CellData(index: 1, title: "Another cell", image: nil, target: self, selector: #selector(anyOtherCellPressed))]
        tableView.reloadData()
    }

    @objc private func firstCellPressed() {
        print("First cell pressed")
    }

    @objc private func anyOtherCellPressed(sender: UIButton) {
        print("Cell at row \(sender.tag) pressed")
    }

}

fileprivate extension MyViewController {

    struct CellData {
        let index: Int
        let title: String?
        let image: UIImage?
        let target: Any?
        let selector: Selector
    }

}

extension MyViewController: UITableViewDataSource {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier", for: indexPath) as! MyTableViewCell // Dequeue cell
        let source = dataSource[indexPath.row] // Get the source object
        cell.button.tag = indexPath.row // Assign tag so we can use it in the button action method

        // Assign target and selector if it exists
        if let target = source.target {
            cell.button.addTarget(target, action: source.selector, for: .touchUpInside)
        }
        cell.cellImageView.image = source.image // Assign image
        cell.label.text = source.title // Assign title
        return cell
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }

}

Still this is far from what I would do but feel free to experiment.

Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • Thank you, your code works like a charm. Is there a way of doing changes to the button when it is tapped. Like it is giving the message in the console. What if I want to change the image of the button, stating that it has been pressed? – Raunak Trikha Jun 02 '17 at 12:13
  • @Trikha Well you need to track the changes. I would say it is best if you track that in your data source. So what you would do is add another property in your class for instance "isPressed" which defaults to false. Then in cell for row you would add the image you need for not pressed. Once the method is triggered you would change the property in data source like dataSource[sender.tag].isPressed = !dataSource[sender.tag].isPressed and reload that single cell. Table view has a method to update cells at index paths... – Matic Oblak Jun 02 '17 at 13:36