Nil数据通过prepareForSegue从tableview详细信息披露传递

时间:2015-02-27 17:24:11

标签: ios uitableview swift

我需要从tableView披露链接传递一个带有prepareForSegue的字符串。这是我使用的prepareForSegue代码。这是我在其他用户需要做同样事情的几种情况下发现的代码,但在我的情况下它不起作用。问题是indexPathForSelectedRow()是nil:

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "alertsDetail" {

        let detailAlertViewController = segue.destinationViewController as DetailAlertViewController
        println(alertTable.indexPathForSelectedRow()) // NIL
        if let indexPath = alertTable.indexPathForSelectedRow() {
            let entryToPass = unpublishedPhotosObjectId[indexPath.row]
            detailAlertViewController.entryId = entryToPass
        }
    }
}

我将tableView命名为“alertTable”而不是self,因为tableview不是视图控制器,只是视图。 如果需要,这里是tableview代码:

 @IBOutlet weak var alertTable: UITableView!
  /////////////TableView - START
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return unpublishedPhotos.count
}

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return 170
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    tableView.separatorColor = UIColor.clearColor()
    let cell = tableView.dequeueReusableCellWithIdentifier("adminAlertsCell") as? adminAlertsCell

    var data = unpublishedPhotos[indexPath.row].getData()
    let image = UIImage(data: data)
    cell!.myImageInCell.image = image
    return cell

}


func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    // Add access to cell
    let cell = tableView.cellForRowAtIndexPath(indexPath) as? adminAlertsCell



        tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        println(cell!.banSwitch.on)

    }
}



/////////////TableView - END

2 个答案:

答案 0 :(得分:1)

您应该从控制器而不是直接从详细信息披露按钮中创建segue。实施方法, tableView:accessoryButtonTappedForRowWithIndexPath:并从该方法内部调用performSegueWithIdentifier:sender :.传递深入作为sender参数,以便您可以访问prepareForSegue中的

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    let indexPath = sender as NSIndexPath
    if segue.identifier == "alertsDetail" {

        let detailAlertViewController = segue.destinationViewController as DetailAlertViewController
        let entryToPass = unpublishedPhotosObjectId[indexPath.row]
        detailAlertViewController.entryId = entryToPass
    }
}

答案 1 :(得分:0)

您的问题的评论中陈述了您的问题,并且有几种解决方案。也许最快,因为你只有一个部分,就是将indexPath.row分配给单元格的标记。然后在准备segue时,发件人将成为您的单元格。你可以从中获得价值。所以在你返回细胞之前的cellForRow方法中:

cell!.tag = indexPath.row

然后在你的prepareForSegue:

let cell = sender as? adminAlertsCell
println(cell!.tag)

这里我只是打印它,但您可以使用它来传递该行。

相关问题