I want to create and return an array of tuples in a tableView function but I am not sure how. I believe one method is to do so by decomposing the tuple but I'm not sure how to execute in this case. I know the last tableView is incorrect as it doesn't return (String, String) that is just my attempt.
class RideHistoryViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
let rideHistory: [(String,String)] = [("Driver: Joe, 12/29/2021", "$26.50"),
("Driver: Sandra, 01/03/2022", "$13.10"),
("Driver: Hank, 01/11/2022", "$16.20"),
("Driver: Michelle, 01/19/2022", "$8.50")]
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UITableViewCell.self,forCellReuseIdentifier:"TableViewCell")
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Code Here
return self.rideHistory.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code Here
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
cell.textLabel?.text = self.rideHistory[indexPath.row]
return cell
}
CodePudding user response:
You can access that tuple value by there index(0,1,2,3).
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Code Here
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath)
cell.textLabel?.text = self.rideHistory[indexPath.row].0
print(self.rideHistory[indexPath.row].0) // Driver: Joe, 12/29/2021
print(self.rideHistory[indexPath.row].1) // $26.50
return cell
}