晚上好。
我在 Swift 中有一个 TableView,它列出了一些项目。我想,当用户点击它时,显示一个新的 ViewController,其中包含有关该项目的详细信息(如名称、照片、描述)。所以,这是我的 tableView 函数:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return listExample.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cellExample", forIndexPath: indexPath) as! ExampleTableViewCell
//Remove separators margins
cell.layoutMargins = UIEdgeInsetsZero
cell.labelExample.text = listExample[indexPath.row]
return cell
}
我做了一些研究,发现也许我可以用那个函数来做到这一点:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
但是,我如何才能将所选项目(在本例中为标签)发送到新的 viewController,例如,我将在其中为标题添加其他标签?
最佳答案
你可以在这个函数中这样做:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let text = listExample[indexPath.row]
self.performSegueWithIdentifier(yourSegueIdentifier, sender: text)
}
然后你覆盖它:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == yourIdentifierAbove {
let controller = segue.destinationViewController as! YourControllerType
let textPass = sender as! String
controller.variableWillReceive = textPass
}
}
关于ios - 在 Swift 的 TableView 中使用 segue 发送标签文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567409/