滑动单元格时,有没有办法从 tableViewCell 导航到另一个视图控制器? [英] Is there a way to navigate from a tableViewCell to another view controller when a cell is swiped?
问题描述
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
}
}
但得到的错误是 CustomTableViewCell 没有名为 performSegueWithIdentifier 的成员.任何人都有任何见解?谢谢!
but got the error that the CustomTableViewCell does not have a member named performSegueWithIdentifier. Anyone have any insight? Thanks!
推荐答案
首先,performSegueWithIdentifier
是一个 UIViewController
方法,所以是一个 UIView
对象将永远无法收到它.
First, performSegueWithIdentifier
is a UIViewController
method, so a UIView
object will never be able to receive it.
第二,我不建议你在滑动的同时切换View Controller,因为这有点违反iOS人机界面原则.人们习惯于通过滑动来删除或编辑它,而不是导航到下一个视图控制器.
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.
但是如果您真的确定要这样做,只需获取当前的 UIViewController 对象即可.但是我认为在视图中获取视图控制器对象并不是一个好的设计.您可能想查看 UITableViewDelegate 协议以找到更好的位置.此外,您还可以利用滑动手势的委托来执行操作.
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.
你不必坚持使用performSegueWithIdentifier
,有很多方法可以切换视图控制器.
You don't necesarrily to insist using performSegueWithIdentifier
, there are lots of ways to switch view controllers.
这篇关于滑动单元格时,有没有办法从 tableViewCell 导航到另一个视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!