我正在制作一个应用程序,其Table View Cell
中有近三个按钮。一个用于共享,另外两个用于其他目的。在添加共享按钮代码之前,一切对我来说都很好,其他两个按钮工作得很好但是现在一旦我编写了共享按钮的功能,我在'Else'
部分中遇到错误{{1声明。这里,单元格通向另一个视图控制器。请帮我理解我哪里出错了。
IndexPath
答案 0 :(得分:2)
我认为您的问题可能是当您点击共享按钮时,单元格不会进入“已选中”状态,因此您调用的方法
self.tableViews.indexPathForSelectedRow!
导致崩溃,因为没有选定的行,你强行打开它。
一种可能的解决方案可能是这样的:
func didPressShareButton(sender: UITableViewCell) {
let indexPath = tableView.indexPathForCell(sender)
// prepare your DetailViewController now / show segue
}
答案 1 :(得分:0)
检查是否实际选择了indexPath。
var indexPath = NSIndexPath()
if let path = tableViews.indexPathForSelectedRow {
indexPath = path
} else {
//Do something if there is no indexPath selected
return//end function
}