ios - 使用prepareForSegue从 TableView 详细信息披露中传递零数据

标签 ios uitableview swift

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

    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 不是 View Controller ,只是一个 View 。 如果需要,这里是表格 View 代码:

 @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

最佳答案

您应该从 Controller 而不是直接从详细信息披露按钮进行转接。实现方法, tableView:accessoryButtonTappedForRowWithIndexPath: 并从该方法内部调用 performSegueWithIdentifier: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
    }
}

关于ios - 使用prepareForSegue从 TableView 详细信息披露中传递零数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28770811/

相关文章:

iphone - 向 UIImageView 添加触摸不起作用

iPhone 表格,您将验证放在哪里?

ios - 我应该使用什么 View Controller 来进行来回通信?

ios - 重写 Swift 中行插入的 withRowAnimation 样式

ios - 如何正确设置导航栏中图像的约束?

ios - 更新到 SDK 1.3.1 后未捕获 GMSMapView 上的拖动/平移手势

ios - 调整 UIPickerView 字体大小

ios - 删除 subview 并将其添加到自定义 UITableViewCell

ios - 有没有办法将 "preprocess"添加到 UIButton?

ios - Swift 解散/展开到模式在 xcode 6 beta 3 中不起作用