Ios 如何在tableview单元格中单击按钮获取标签以进行PrepareForegue
我有一个桌面视图。我需要从该单元格中获取标签“usernameLabel”,并为其分配一个变量。我需要将该变量传递给prepareForSegue问题是标签是来自错误单元格的错误标签。 以下是我所拥有的:Ios 如何在tableview单元格中单击按钮获取标签以进行PrepareForegue,ios,swift,xcode,uitableview,segue,Ios,Swift,Xcode,Uitableview,Segue,我有一个桌面视图。我需要从该单元格中获取标签“usernameLabel”,并为其分配一个变量。我需要将该变量传递给prepareForSegue问题是标签是来自错误单元格的错误标签。 以下是我所拥有的: var username: String! func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell :
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传递给另一个视图控制器。但目前,它从错误的单元格中获取标签。不应将状态数据保存在单元格中。您应该在模型中拥有所需的信息(可能是一个数组)。当用户点击单元格时,您应该使用所选单元格的indexPath从模型中获取信息,而不是从单元格上的标签中获取信息
(在MVC设计模式中查找背景。表视图单元格是视图对象,不应存储数据。这是模型的工作。)
cellforrowatinexpath
方法将重复多次,因此在该方法中赋值将不起作用,另外,为什么您要在按钮上使用tapperstate
,而不是在按钮上添加操作,请尝试像这样更改cellforrowatinex
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : MainCell! = tableView.dequeueReusableCellWithIdentifier("MainCell") as! MainCell
cell.button.setTitle( usernameLabel.text, forState: .Normal)
cell.button.addTarget(self, action: #selector(self.buttonAction(_:)), forControlEvents: .TouchUpInside)
return cell
}
现在在ViewController
func buttonAction(sender: UIButton) {
let center = sender.center
let point = sender.superview!.convertPoint(center, toView:self.tableView)
let indexPath = self.tableView.indexPathForRowAtPoint(point)
let cell = self.tableView.cellForRowAtIndexPath(indexPath) as! MainCell //Add superview on the basis of your button hierarchy in the cell
username = cell.usernameLabel.text
print(username)
performSegueWithIdentifier("ViewToView2Segue", sender: self)
}
username=usernamelab.text
是每个单元格的usernamelab
?如果是这样,您将需要cell.usernamelab.text
。但是你想做什么呢?您需要从标签传递内容,对吗?@SeanWang我需要通过segue将label.text传递给另一个视图控制器。但目前,它从错误的单元格中获取标签。很高兴你删除了关于它是多么混乱的一段。我意识到我第一眼看到的代码是错误的,所以该段的一部分是错误的。但是代码仍然很混乱。模型文件的示例是什么?使用sender.superview!的代码!。超级视图代码>是脆弱的。它依赖于单元格的当前布局。例如,如果在新的父视图中移动按钮,则此代码将中断。最好使用按钮的边界,将其从坐标空间转换为表视图的坐标空间,并询问表视图哪个单元格包含该按钮。我对此的代码在Objective-C中,或者我会将其作为一个单独的答案发布。这是我得到的错误:-[UITapGestureRecognizer superview]:未识别的选择器发送到实例
检查编辑的答案。还可以使用添加目标来删除TapSirtation代码。什么是按钮中心
?我得到了以下错误:对成员“tableView(uuu:numberOfRowsInSection:)”的引用不明确。