所以这就是我制作滑动单元的方式:Custom Table View Cell Not Swiping。无论如何,我想知道当向左滑动单元格时是否有从表视图转换到另一个视图控制器的方法。我知道可以在轻触一个单元格并在didSelectRowAtIndexPath中调用performSegueWithIdentifier但从未见过刷过单元格的示例时完成。我尝试将self.performSegueWithIdentifier("detail", sender: self)
添加到滑动逻辑
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的成员。有人有任何见解吗? 谢谢!
答案 0 :(得分:1)
首先,performSegueWithIdentifier
是UIViewController
方法,因此UIView
对象永远无法接收它。
其次,我不建议您在翻转时切换View Controller,因为它违反了iOS人机界面原则。人们习惯于滑动删除或编辑它,而不是导航到下一个视图控制器。
但如果您确实想要这样做,只需获取当前的UIViewController对象,就可以了。但是我认为在视图中获取视图控制器对象并不是一个好的设计。您可能想看看UITableViewDelegate协议以找到更好的地方。此外,您可以利用滑动手势的代表来执行您的操作。
您不必坚持使用performSegueWithIdentifier
,有很多方法可以切换视图控制器。
答案 1 :(得分:0)
这是因为theCustomTableViewCell
没有名为performSegueWithIdentifier
的方法,但是UIViewController有这个方法
尝试委派UITableViewDelegate
并使用此方法单独获取项目:
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
}