stefan s stefan s - 2 months ago 7
Swift Question

connect custom tableviewcell to Uitableview

I am new in programming so I really need ur help. Basically I have very basic table view controller with custom cell inside.

import UIKit

class RestaurantTableViewController: UITableViewController {
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "Cafe Loisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh's Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "Thai Cafe"]

var restaurantImages = ["cafedeadend.jpg", "homei.jpg", "teakha.jpg", "cafeloisl.jpg", "petiteoyster.jpg", "forkeerestaurant.jpg", "posatelier.jpg", "bourkestreetbakery.jpg", "haighschocolate.jpg", "palominoespresso.jpg", "upstate.jpg", "traif.jpg", "grahamavenuemeats.jpg", "wafflewolf.jpg", "fiveleaves.jpg", "cafelore.jpg", "confessional.jpg", "barrafina.jpg", "donostia.jpg", "royaloak.jpg", "thaicafe.jpg"]

var restaurantLocations = ["Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Hong Kong", "Sydney", "Sydney", "Sydney", "New York", "New York", "New York", "New York", "New York", "New York", "New York", "London", "London", "London", "London"]

var restaurantTypes = ["Coffee & Tea Shop", "Cafe", "Tea House", "Austrian / Causual Drink", "French", "Bakery", "Bakery", "Chocolate", "Cafe", "American / Seafood", "American", "American", "Breakfast & Brunch", "Coffee & Tea", "Coffee & Tea", "Latin American", "Spanish", "Spanish", "Spanish", "British", "Thai"]

override func viewDidLoad() {
super.viewDidLoad()

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK: - Table view data source

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

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

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

let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! RestaurantTableViewCell

// Configure the cell...
cell.nameLabel.text = restaurantNames[(indexPath as NSIndexPath).row]
cell.thumbnailImageView.image = UIImage(named: restaurantImages[(indexPath as NSIndexPath).row])
cell.locationLabel.text = restaurantLocations[(indexPath as NSIndexPath).row]
cell.typeLabel.text = restaurantTypes[(indexPath as NSIndexPath).row]


return cell


Also i have 4 sep table view controllers. My question is how can I establish segues so when I press for example the first cell - "a" tableviewcontroller opened. When I pressed second cell -"b"tableviewcontroller opened.

I am very very new in programming, so please guys,write everything step by step.
Thank you so much for the HELP!!!

Answer

The other responses are developed for Swift2 but you want it for Swift3 that's why you can't find the methods.

So I am going to provide you a very basic example of navigation between TableViewControllers hopefully it can help you to understand how the navigation works so that you implement it in your project.

Result

Code

class TableViewController: UITableViewController {
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = "Go to Table \(indexPath.row + 1)"
    return cell
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let numberOfRow = indexPath.row

    switch numberOfRow{
    case 0:
      performSegue(withIdentifier: "showA", sender: numberOfRow)
    case 1:
      performSegue(withIdentifier: "showB", sender: numberOfRow)
    default:
      break;
    }
  }

  // This method will be useful to you
  // in case you want to send any info
  // to your destination view controllers.
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showA" {
      let tableViewControllerA = segue.destination as! TableViewControllerA
      tableViewControllerA.someData = sender as? Int
    } else if segue.identifier == "showB" {
      let tableViewControllerB = segue.destination as! TableViewControllerB
      tableViewControllerB.someData = sender as? Int
    }
  }
}

class TableViewControllerA: UITableViewController {
  var someData: Int?

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Table 1"
  }
}

class TableViewControllerB: UITableViewController {
  var someData: Int?

  override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "Table 2"
  }
}

Additional Info:

enter image description here


The way you do it is like below:

enter image description here


enter image description here


enter image description here


enter image description here