【发布时间】:2016-12-20 09:17:11
【问题描述】:
我有一个表格视图。我需要从该单元格中获取标签“usernameLabel”并为它们分配一个变量。我需要将该变量传递给prepareForSegue。 问题是标签是来自错误单元格的错误标签。
这是我所拥有的:
var username: String!
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : MainCell! = tableView.dequeueReusableCellWithIdentifier("MainCell") as! MainCell
username = usernameLabel.text
cell.button.userInteractionEnabled = true
let tapButton = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapLabel(_:)))
cell.button.addGestureRecognizer(tapButton)
return cell as MainCell
}
func tapButton(sender:UITapGestureRecognizer) {
performSegueWithIdentifier("ViewToView2Segue", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ViewToView2Segue" {
let userProfileViewController = segue.destinationViewController as! SecondViewController
secondViewController.usernamePassed = usernamePassed
}
}
简化问题: 我需要通过 segue 将 label.text 传递给另一个视图控制器。但目前,它正在从错误的单元格中获取标签。
【问题讨论】:
-
username = usernameLabel.text是usernameLabel每个单元格吗?如果是这样,您将需要cell.usernameLabel.text。但是你想做什么?你需要从标签中传递内容,对吗? -
@SeanWang 我需要通过 segue 将 label.text 传递给另一个视图控制器。但目前,它正在从错误的单元格中获取标签。
标签: ios swift xcode uitableview segue