I am trying to display two arrays in two different cells in a table view but it doesn't works , can any one help me to solve this problem , I am using Xcode8 and Swift3 , and this is my TableView code .
this is how I want to display the two arrays :
row1 = aaa
row2 = 11111
row3 = bbb
row4 = 2222
row5 = cccc
row6 = 3333
row7 = ddd
row8 = eee
row9 = fff
My code :
import UIKit
class _CellsTableViewController: UITableViewController {
var array1 = [String]()
var array2 = [String]()
override func viewDidLoad() {
super.viewDidLoad()
array1 = ["aaa","bbb","ccc","ddd","eee","fff"]
array2 = ["11111","22222","33333"]
}
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 {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return array1.count + array2.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = array1[indexPath.row]
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1", for: indexPath)
cell.textLabel?.text = array2[indexPath.row]
return cell
}
}
}
if indexPath.row == 0would do this? Seems a basic programming issue.if indexPath.row == 0 {, it is the one index and at the same time your array count is 9 but you load different ok simple this isif indexPath.row == 0 {orif indexPath.section == 0 {