# Add button to uitableview cell programmatically

**URL:** <https://forums.kodeco.com/t/add-button-to-uitableview-cell-programmatically/113327>\
**Category:** Swift\
**Created:** [June 27, 2020, 3:11pm UTC](https://forums.kodeco.com/t/add-button-to-uitableview-cell-programmatically/113327 "2020-06-27T15:11:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timswift](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/timswift/32/110937_2.png) [@timswift](https://forums.kodeco.com/u/timswift)\
**Post date:** [June 27, 2020, 3:11pm UTC](https://forums.kodeco.com/t/add-button-to-uitableview-cell-programmatically/113327/1 "2020-06-27T15:11:45Z")

</div>

I just want my code to produce a button in every table view cell that has text. When the button is press just have it say hi. That is all thanks.

```auto
import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    private let myArray: NSArray = ["First","Second","Third"]
     var myTableView = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()

        myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "MyCell")
        myTableView.dataSource = self
        myTableView.delegate = self
        
        
        
        self.view.addSubview(myTableView)
        myTableView.translatesAutoresizingMaskIntoConstraints = false
        
        
        
        NSLayoutConstraint.activate([

            myTableView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.90),
            myTableView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 1),
            myTableView.topAnchor.constraint(equalTo: view.topAnchor),
            myTableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),

        ])
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("Num: \(indexPath.row)")
        print("Value: \(myArray[indexPath.row])")
    }

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
        cell.textLabel!.text = "\(myArray[indexPath.row])"
        return cell
    }
}
```

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 8, 2020, 2:56am UTC](https://forums.kodeco.com/t/add-button-to-uitableview-cell-programmatically/113327/2 "2020-07-08T02:56:20Z")

</div>

@timswift Do you still have issues with this?

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [December 11, 2020, 7:11am UTC](https://forums.kodeco.com/t/add-button-to-uitableview-cell-programmatically/113327/3 "2020-12-11T07:11:45Z")

</div>

Closed on December 11, 2020, 7:11am UTC

This topic was automatically closed after 166 days. New replies are no longer allowed.
