我正在为我的应用程序实现commentView。我有一个主视图,它是包含图片的表格 View 和一个用于评论 View 的按钮。 我希望当用户点击 TableView 中的评论按钮时, View 显示评论 View 并通过prepareforSegue方法传递PFObject。
现在评论按钮可以使用,但我有来自prepareforsegue的错误 这是我的代码。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "mainToComment") {
let destViewController : CommentVC = segue.destinationViewController as! CommentVC
destViewController.parentObjectID = parentObjectID
let selectedRowIndex = self.tableView.indexPathForSelectedRow
destViewController.object = (postsArray[(selectedRowIndex?.row)!] as? PFObject)
这是我的按钮的工作原理。
@IBAction func commentButtonTapped(sender: AnyObject) {
let button = sender as! UIButton
let view = button.superview!
let cell = view.superview as! MainTVCE
let indexPath = tableView.indexPathForCell(cell)
parentObjectID = postsArray[(indexPath?.row)!].objectId!!
当我调试时,selectedRowIndex没有值(nil) 我认为这是因为我点击按钮而不是单元格。
如何为此设置indexPath? 或者 我怎样才能让它发挥作用?
最佳答案
我不知道您的主 TableViewCell View Controller 的名称。假设,我将此 View Controller 命名为 MainTableViewCell
。
我在MainTableViewCell
中创建了一个闭包:
var didRequestToShowComment:((cell:UITableViewCell) -> ())?
点击按钮评论时:
@IBAction func commentButtonTapped(sender: AnyObject) {
self.didRequestToShowComment?(self) // self is this UITableViewCell
}
在主视图 Controller 的table cellForRowAtIndex...
中。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
mainTableViewCell.didRequestToShowComment = { (cell) in
let indexPath = tableView.indexPathForCell(cell)
let objectToSend = postsArray[indexPath.row] as? PFObject
// Show your Comment view controller here, and set object to send here
}
...
return cell
}
关于swift - 我想在点击 tableView Cell 中的按钮时发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193391/