从TableView

时间:2016-01-18 15:05:44

标签: ios xcode swift uitableview

我在这里看到过两个类似的问题,但他们的答案都没有对我有所帮助。 我有一个 tableView 的评论,我想对评论的细节执行一个细节(有点像twitter,如果你点击推文,你有一个详细的视图)。但是给予详细视图的信息是选择的倒数第二行而不是最后选择的。如果您只选择一个,则不会执行segue。

func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
    dispatch_async(dispatch_get_main_queue()) {
        self.performSegueWithIdentifier("detail_segue", sender: indexPath)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if(segue.identifier == "detail_segue"){
        let row = (sender as! NSIndexPath).row;
        let commentForSegue = self.AOS[row]
        let destinationVC = segue.destinationViewController as! CommentDetailVC
        destinationVC.detail_title = commentForSegue.titulo_comment
        destinationVC.detail_body = commentForSegue.cuerpo_comment
        destinationVC.detail_date = commentForSegue.fecha_comment
        destinationVC.detail_num_agree = String(commentForSegue.num_agrees)
        destinationVC.detail_num_disagree = String(commentForSegue.num_disagrees)
        destinationVC.detail_agreed = commentForSegue.agreed
    } 
}

我在dispatch_asyncprepareForSegue上使用和不使用didSelectRowAtIndexPath两种情况都尝试过,但它不起作用。我也尝试过didSelectRowAtIndexPath的所有工作,但也没有成功。

谢谢!

1 个答案:

答案 0 :(得分:1)

首先,您需要在方法didSelectRowAtIndexPath中调用segue,并且您从方法didDeselectRowAtIndexPath调用它时两者之间存在一点差异,但是需要一些提示最后一个单元格也被点击,请参阅以下代码:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // the identifier of the segue is the same you set in the Attributes Inspector
    self.performSegueWithIdentifier("detail_segue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

   if(segue.identifier == "detail_segue"){
       // this is the way of get the indexPath for the selected cell
       let indexPath = self.tableView.indexPathForSelectedRow()
       let row = indexPath.row

       let commentForSegue = self.AOS[row]
       let destinationVC = segue.destinationViewController as! CommentDetailVC
       destinationVC.detail_title = commentForSegue.titulo_comment
       destinationVC.detail_body = commentForSegue.cuerpo_comment
       destinationVC.detail_date = commentForSegue.fecha_comment
       destinationVC.detail_num_agree = String(commentForSegue.num_agrees)
       destinationVC.detail_num_disagree = String(commentForSegue.num_disagrees)
       destinationVC.detail_agreed = commentForSegue.agreed
   } 
}

我希望这对你有所帮助。