【问题标题】:How to get label on button click in tableview cell for prepareForSegue如何在准备ForSegue的tableview单元格中单击按钮上的标签
【发布时间】: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.textusernameLabel 每个单元格吗?如果是这样,您将需要cell.usernameLabel.text。但是你想做什么?你需要从标签中传递内容,对吗?
  • @SeanWang 我需要通过 segue 将 label.text 传递给另一个视图控制器。但目前,它正在从错误的单元格中获取标签。

标签: ios swift xcode uitableview segue


【解决方案1】:

您不应将状态数据保存在单元格中。您应该在模型中拥有所需的信息(可能是一个数组)。当用户点击一个单元格时,您应该使用所选单元格的 indexPath 从模型中获取信息,而不是从单元格上的标签中获取信息。

(查找背景的 MVC 设计模式。表格视图单元格是一个视图对象,不应存储数据。这是模型的工作。)

【讨论】:

  • 很高兴你删除了关于它是多么混乱的段落。
  • 我意识到我第一眼就误解了我正在阅读的代码,所以那段的一部分是不正确的。代码仍然是一团糟。
  • 模型文件的示例是什么?
【解决方案2】:

cellForRowAtIndexPath 方法将被多次使用,因此在该方法中分配值将不起作用,还有你为什么在按钮上使用tapGesture 而不是向按钮添加操作,尝试像这样更改你的cellForRowAtIndex

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 中添加这个 buttonAction 函数

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)
}

【讨论】:

  • 您使用sender.superview!.superview! 的代码很脆弱。它依赖于单元格的当前布局。例如,如果您在新的父视图中移动按钮,则此代码将中断。最好使用按钮的边界,将其坐标空间转换为表格视图的坐标空间,然后询问表格视图哪个单元格包含该按钮。我为此提供的代码在 Objective-C 中,或者我会将其作为单独的答案发布。
  • 这是我得到的错误:-[UITapGestureRecognizer superview]: unrecognized selector sent to instance
  • 检查编辑的答案。也可以使用 add target 删除 Tapgesture 代码。
  • 什么是buttonCenter
  • 我收到此错误:Ambiguous reference to member 'tableView(_:numberOfRowsInSection:)'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多