swift - 我想在点击 tableView Cell 中的按钮时发送数据

标签 swift parse-platform

我正在为我的应用程序实现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/

相关文章:

ios - 一次异步从多个配置文件下载数据

swift - 一个函数返回另一个函数的好例子是什么?

swift - 在 Xcode 中使用 Parse 和 Swift 连接 PFUITableView 和详细信息 View

android - 不幸的是应用程序已停止,doinbackground() 出错

iphone - 使用 nib 自定义单元格和自动布局滚动跳跃(ios 8 swift - UITableview)

ios - 在可重复使用的单元格中显示下载进度

ios - 如何在我的 iOS 应用程序中调用 Cloud Code Function(Parse)?

ios - Pickerview 委托(delegate) UIView : array index out of range

ios - 如何使用点击手势 (iOS8/Swift) 在 pageViewController 上显示/隐藏状态栏

ios - 如何使用 AppSync S3ObjectManager 更新 View 的进度条?