Reputation:
I have attached my code below.
While executing I am getting only one section as output but it should return 3 sections in table view. I don't know what I should do.
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var itemsInSections: Array<Array<String>> = [["1A", "1B", "1C"], ["2A", "2B"], ["3A", "3B", "3C", "3D", "3E"]]
var sections: Array<String> = ["Section 1", "Section 2", "Section 3"]
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.dataSource = self
self.tableView.delegate = self
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.itemsInSections[section].count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return self.sections[section]
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let text = self.itemsInSections[indexPath.section][indexPath.row]
cell.textLabel!.text = text
return cell
}
}
Upvotes: 0
Views: 135
Reputation: 1298
You have wrong method
Replace Old Swift Syntax
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return self.sections.count
}
With New Swift Syntax
func numberOfSections(in tableView: UITableView) -> Int {
return self.sections.count
}
Upvotes: 4