1

How do I programmatically add cells to a UITableview and fill the cells with data from myArray[cellNumber]. The data in the array is of type String. The tableview is just an UITableView connected with an outlet.

All the examples I've found is either +30 lines or doesn't work... I'm using swift 4 and UIKit.

Uma Madhavi
  • 4,851
  • 5
  • 38
  • 73
Harcker
  • 151
  • 1
  • 13
  • possible duplicate https://stackoverflow.com/questions/40220905/create-uitableview-programatically-in-swift/40221235#40221235 – Joe Feb 14 '18 at 18:15
  • Not a duplicate I’m asking for how to create a cell, the link is about creating the tableview itself – Harcker Feb 14 '18 at 19:17

2 Answers2

0
  1. In Xcode, use "File > New > File > Cocoa Touch Class".
  2. Use UITableViewController as a base class
  3. You will find a big template, just implement:

    • numberOfSections(in tableView: UITableView) -> Int, make it return 1. You just need one section for now.
    • tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int, make it return the size of your array
    • override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell. Uncomment it, implement it.

      NOTE: To implement tableView(_:cellForRowAt:), you must in your storyboard register a cell, and use its name in this function. Or programmatically register a cell using register(_:forCellReuseIdentifier:) .

Here is a more comprehensive guide iOS Getting Started Guide UITableView

Implementation example:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1  // Only one section
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    // "cell" is registered in the Storyboard
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    // The registered cell, has a view with tag 1 that is UILabel as an example
    // IndexPath is a data structure that has "section" and "row"
    // It located the cell in your tableview/collectionview
    (cell.viewWithTag(1) as? UILabel)?.text = myArray[indexPath.row]

    return cell
}
user9335240
  • 1,739
  • 1
  • 7
  • 14
0

1.Your ViewController must conform to the UITableViewDelegate, UITableViewDataSource. That means your class file would look like this

class MyCustomViewController: UIViewController, UITableViewDelegate, UITableViewDataSource

2.You must assign the dataSource and delegate properties of your UITableView object to your viewController either from the Storyboard by dragging or in the code in viewDidLoad for example by typing:

myTableView.delegate = self
myTableView.dataSource = self

3.Your class must override the UITableView required delegates/datasource methods numberOfRowsInSection and cellForRowAt:

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

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

Note that to use dequeReusableCell you must set a Reuse Identifier for the cell in the storyboard file.

i.bel
  • 116
  • 1
  • 5