我的UITableView
个单元格设置为UIButton
,可以转到我的DetailVC。 我不想基于完整的小区选择进行segue,而是完全通过按钮进行。我的问题是我需要通过这个segue传递一个特定于我的单元格的JSON
密钥。在这种情况下,每个单元格都有一个特定的“id”,我需要将其作为变量传递给DetailVC,以便稍后在http请求中使用。
有没有办法通过@IBAction
?
以下是我的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 = ""
非常感谢任何帮助!谢谢!
答案 0 :(得分:1)
这个怎么样? (所有代码都是在没有编译的情况下编写的,因此您可能需要稍微调整一下)。 将行号存储为按钮的标记。然后,您就可以使用它来检索ID。
1)在CustomCell中的按钮上添加@IBOutlet
。
@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
}
}
}
如果您的目标视图控制器嵌入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
}
}
}