问题
So here's how I made my swiping cells: Custom Table View Cell Not Swiping. Anyway I was wondering if there was a way of transitioning from a table view to another view controller when a cell is swiped leftwards. I know it can be done when a cell is tapped and calling performSegueWithIdentifier in didSelectRowAtIndexPath but have never seen an example of when a cell is swiped. I tried adding self.performSegueWithIdentifier("detail", sender: self) to the logic of the swipe
if recognizer.state == .Ended {
// the frame this cell had before user dragged it
let originalFrame = CGRect(x: 0, y: frame.origin.y,
width: bounds.size.width, height: bounds.size.height)
if !reserveOnDragRelease {
// if the item is not being deleted, snap back to the original location
UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
} else {
UIView.animateWithDuration(0.2, animations: {self.frame = CGRectMake(-self.bounds.size.width, originalFrame.origin.y, originalFrame.width, originalFrame.height)})
// Added it here
}
}
but got the error that the CustomTableViewCell does not have a member named performSegueWithIdentifier. Anyone have any insight? Thanks!
回答1:
First, performSegueWithIdentifier is a UIViewController method, so a UIView object will never be able to receive it.
Second, I don't recommend you to do switch View Controller while swipping, because it is kind of violation to iOS human-machine interface principles. People get used to swipe to delete or edit it, not navigating to next view controller.
But If you are really sure you want to do this, simply get your current UIViewController object and you are good to go. But getting view controller object inside a view is not a good design I think. You probably want to take a look at the UITableViewDelegate protocol to find a better place. Also, you could leverage your swipe gesture's delegate to do your actions.
You don't necesarrily to insist using performSegueWithIdentifier, there are lots of ways to switch view controllers.
回答2:
It is because theCustomTableViewCell doesn't have a method called performSegueWithIdentifier but UIViewController has that method
Try to delegate UITableViewDelegate and use this method to get item individually:
myTableView.delegate = self
////////////////////////////
var myItems: [String: Bool] = [:]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items.objectAtIndex(indexPath.row) as String
let itemId = selectedItem.componentsSeparatedByString("$%^")
// add to self.selectedItems
myItems[itemId[1]] = true
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
let selectedItem = items.objectAtIndex(indexPath.row) as String
let itemId = selectedItem.componentsSeparatedByString("$%^")
// remove from self.selectedItems
myItems[itemId[1]] = nil
}
来源:https://stackoverflow.com/questions/30367187/is-there-a-way-to-navigate-from-a-tableviewcell-to-another-view-controller-when