ios - 在 Swift 中不使用 didSelectRowAtIndexPath 在 DetailView 中引用特定于单元格的 JSON 键

标签 ios json uitableview swift segue

我的 UITableView 单元格设置有一个 UIButton,它会转到我的 DetailVC。 我不想基于完整的单元格选择进行搜索,而是完全通过按钮进行搜索。我的问题是我需要通过此 segue 传递特定于我的单元格的 JSON key 。在这种情况下,每个单元格都有一个特定的 “id”,我需要将其作为变量传递给 DetailVC,以便稍后在 http 请求中使用。

有没有办法通过 @IBAction 进行 segue 和传递 key ?

下面是我的 cellForRowAtIndexPath 函数。我需要将 “needID” 或键 “id” 传递给我的 DetailView。

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:CustomCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as CustomCell

    var rowData: NSDictionary = dataArray[indexPath.row] as NSDictionary
    var needID=rowData["id"] as String
    var firstName=rowData["needFirstname"] as String
    var descrip=rowData["needDescription"] as String
    var poster=rowData["needPoster"] as String
    var city=rowData["needCity"] as String
    var state=rowData["needState"] as String
    var country=rowData["needCountry"] as String

    cell.needID.text = needID
    cell.needFirstName.text = firstName
    cell.needDescription.text = descrip; cell.needDescription.numberOfLines = 0
    cell.needPoster.text = poster
    cell.needCity.text = city
    cell.needState.text = state
    cell.needCountry.text = country

    return cell
}

在我的 DetailVC 中,我已经准备好 id 以填充以下变量:

var passedID:String = ""

非常感谢任何帮助!谢谢!

最佳答案

这个怎么样? (所有代码都是在没有编译的情况下编写的,因此您可能需要稍微调整一下)。 将行号存储为按钮的标签。然后您就可以使用它来检索 ID。

1) 将 @IBOutlet 添加到您的 CustomCell 中的按钮。

@IBOutlet myButton:UIButton!

2) 在cellForRowAtIndexPath

cell.myButton.tag = indexPath.row

3) 在prepareForSegue中:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "myCellButtonSegue" {
        if let mybutton = sender as? UIButton {
            let rowData: NSDictionary = dataArray[mybutton.tag] as NSDictionary
            let dvc = segue.destinationViewController as DetailViewController
            dvc.passedID = rowData["id"] as String
        }
    }
}

如果您的目标 View Controller 嵌入到 UINavigationController 中,我相信您应该这样做:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "myCellButtonSegue" {
        if let mybutton = sender as? UIButton {
            let rowData: NSDictionary = dataArray[mybutton.tag] as NSDictionary
            let navcon = segue.destinationViewController as UINavigationController
            let dvc = navcon.topViewController as DetailViewController
            dvc.passedID = rowData["id"] as String
        }
    }
}

关于ios - 在 Swift 中不使用 didSelectRowAtIndexPath 在 DetailView 中引用特定于单元格的 JSON 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25594264/

相关文章:

ios - Xcode 7 中的 UITest : How to test keyboard layout (keyboardType)

python - 无法正确读取json文件

json - 在Grails中编码Joda LocalTime

php - 将 php 数组移动到 jquery 中进行操作

iPhone UITableView - 附件点击 Action

iphone:如何在 UITableView 中制作多列

ios - 无法为不匹配的人脸定位 Face Id 弹出窗口

ios - 检查两个节点相互接触时颜色是否相同

ios - 如何水平和垂直滚动 CollectionView 单元格..我正在使用 9 个单元格 CollectionView

ios - 在 UITableView 中单击的事件