You can get tableview in .storyboard either by using TableViewcontroller or ( drag and drop Table View from object library in normal ViewController, drag Table View cell under Table View.
Select Table View Cell in .storyboard -> go to attributes pane -> Rename Identifier to something unique Example "cellReuseIdentifier"
Make tableView IBOutlet named IBOutletTableView
Add below lines in viewDidLoad method
for i in 0...5{
data.append("\(i)")
}
IBOutletTableView.dataSource = self
Make your class delegate of UITableViewDataSource
class ViewController: UITableViewController {
Add below functions in ViewController class
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")!
let text = data[indexPath.row]
cell.textLabel?.text = text
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section No: \(section)"
}
IMPORTANT: BIG FLAW in TableView. when you slide below in tableview then new cells are not created only the cellForRowAt function is called to change the property of the cell in that position.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! as! CustomTableViewCell
let text = data[indexPath.row]
cell.paramNameLabel.text = text
cell.paramValueLabel.text = text
if indexPath.row%2 == 0{
cell.contentView.backgroundColor = UIColor.yellow
}
return cell
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! as! CustomTableViewCell
let text = data[indexPath.row]
cell.paramNameLabel.text = text
cell.paramValueLabel.text = text
if indexPath.row%2 == 0{
cell.contentView.backgroundColor = UIColor.yellow
}
else{
cell.contentView.backgroundColor = UIColor.white
}
return cell
}
```
8. Obtain the cell from indexPath by below function
let cell = tableView.cellForRow(at: indexPath)!
let cell = tableView.dequeueReusableCell(withIdentifier: "cellReuseIdentifier")! as! CustomTableViewCell
cell.label.text = "name"
paramTableView.register(<#T##nib: UINib?##UINib?#>, forCellReuseIdentifier: <#T##String#>)
class ExampleCell: UITableViewCell {
//create your closure here
var buttonPressed : (() -> ()) = {}
@IBAction func buttonAction(_ sender: UIButton) {
//Call your closure here
buttonPressed()
}
}
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ExampleCell", for: indexPath) as! ExampleCell
cell.buttonPressed = {
//Code
}
return cell
}
}
Make ViewController as delegate of UITableViewDelegate
class ParameterTuningViewController: UITableViewController, UITableViewDelegate
Add below line in ViewDidLoad
IBOutletTableView.delegate = self
add below function to present alert controller when selected a table view cell
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let alertController = UIAlertController(title: "Hint", message: "You have selected row no \(indexPath.row) and section no \(indexPath.section)", preferredStyle: .alert)
let alertAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(alertAction)
present(alertController, animated: true, completion: nil) // UITableViewDelegate
}