If I have 2-3 TableView, how can I disable 'Delete' row only for the specific TableView? When I set breakpoint for if statement to check which tableView is used, it's not working
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if tableView == self.firstTableView {
if editingStyle == .delete {
array.remove(at: indexPath.row)
firstTableView.deleteRows(at: [indexPath], with: .fade)
firstTableView.reloadData()
}
}
}
I tried to set editing mode to false in viewDidLoad for secondTableView but it's not working also.
secondTableView.setEditing(false, animated: false)
I understand that by default it's set to false, so I thought if commit editingStyle
enable it for all tableViews, so I can disable it for second.
Just give each TableView
a tag and check it in an if
or switch
statement.
if tableView.tag == 0 {
if editingStyle == .delete {
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
}
The correct answer is checking for tag in editingStyleForRowAt indexPath
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
if tableView.tag == 1 {
return UITableViewCellEditingStyle.delete
} else {
return UITableViewCellEditingStyle.none
}
}
You can use:
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item or table to be editable.
if tableView == tableVw {
return false
} else {
return true
}
}
Here tableVw is a tableview
object which you want to not editable or you can also use tag instead of object compare. Then use:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
//Write your delete cell logic here
array.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
tableView.reloadData()
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.