问题
So I have an app(written in Swift) that allows a user to search for golf courses in a searchController. They can then select that course and then that selected course goes into a tableView. I'm using the same view controller for both the search controller and the list of the selected courses.
The issue I'm having is I want the user to have the ability to swipe to delete their selected courses but NOT the courses that populate search controller when it is active. The swipe to delete action is working in the regular tableView. When the searchController is active I have it to the point where the delete button is disabled. BUT what I want to do is not have the cells be able to slide at all if the searchController is active. Can't seem do get it work. Any suggestions? Please and thank you.
Here is my code:
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if searchController.active {
UITableViewCellEditingStyle.None
} else {
let deletedValue = previousCoursesFromRealm[indexPath.row]
if editingStyle == UITableViewCellEditingStyle.Delete {
let realm = try! Realm()
try! realm.write {
realm.delete(deletedValue)
}
coursesTableView.reloadData()
}
}
}
回答1:
I think the method you're looking for is tableView(_:canEditRowAtIndexPath:).
Returning false
in this method should disable the swipe action. You can then tie this value to your search controller:
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return !searchController.active
}
来源:https://stackoverflow.com/questions/34550291/disable-cell-swipe-action