iOS tableView delegate methods in swift 3
The below are the new table view delegate methods in Swift3.
You can display the number of sections here.Now I used as a single section.
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return array.count
}
You can configure the cell using this method.you can design a customized cell and connect their properties and use it here
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCell
return cell
}
Based on the cell selection you can perform the operations here.Maximum you can call the nextViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.storyboard?.instantiateViewController(withIdentifier: "Cell") as! ViewController
self.navigationController?.pushViewController(cell, animated: true)
}
It will return the possibility of a cell can be editable or non-editable.If true it will be editable.Otherwise nonEditable
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
Editing style will be used here.If you want to delete the cell then you can use alert view controller to get the confirmation from the user.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
let alert = UIAlertController(title: AppTitle, message: "title", preferredStyle: .alert)
let okAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.default) {
UIAlertAction in
//paste the action here
}
}
let cancelAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
alert.addAction(okAction)
alert.addAction(cancelAction)
// Present the controller
self.present(alert, animated: true, completion: nil)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
You can display the number of sections here.Now I used as a single section.
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return array.count
}
You can configure the cell using this method.you can design a customized cell and connect their properties and use it here
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! MyCell
return cell
}
Based on the cell selection you can perform the operations here.Maximum you can call the nextViewController
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = self.storyboard?.instantiateViewController(withIdentifier: "Cell") as! ViewController
self.navigationController?.pushViewController(cell, animated: true)
}
It will return the possibility of a cell can be editable or non-editable.If true it will be editable.Otherwise nonEditable
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
Editing style will be used here.If you want to delete the cell then you can use alert view controller to get the confirmation from the user.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
let alert = UIAlertController(title: AppTitle, message: "title", preferredStyle: .alert)
let okAction = UIAlertAction(title: "YES", style: UIAlertActionStyle.default) {
UIAlertAction in
//paste the action here
}
}
let cancelAction = UIAlertAction(title: "NO", style: UIAlertActionStyle.cancel) {
UIAlertAction in
}
alert.addAction(okAction)
alert.addAction(cancelAction)
// Present the controller
self.present(alert, animated: true, completion: nil)
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
I have read this post. Collection of post is a nice one ios swift online training
ReplyDelete