make a table view in swift

To create a table view in Swift, follow these steps:

  1. Open Xcode and create a new project.
  2. Select "Single View App" as the template.
  3. Enter a name for your project and select your desired options.
  4. Open the Main.storyboard file and drag a UITableView onto the view.
  5. Set the table view's dataSource and delegate to your view controller.
  6. In your view controller's Swift file, adopt the UITableViewDelegate and UITableViewDataSource protocols.
  7. Implement the required tableView(:numberOfRowsInSection:) and tableView(:cellForRowAt:) methods in your view controller.
  8. Customize the appearance and behavior of your table view as desired.

Here's an example of how to implement the two required methods in your view controller:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    let items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]

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

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

This code sets up a table view with five rows, each displaying a string from the items array. Make sure to set the identifier of your table view cell to "cell" in the storyboard file.

gistlibby LogSnag