【问题标题】:Xcode6 swift UIButton sender not unique?Xcode6 swift UIButton 发件人不是唯一的吗?
【发布时间】:2015-01-13 16:29:03
【问题描述】:

我只是想知道为什么 UIButton 的发送者不是唯一的或变化的。我有一个动态填充的表格视图(大约 100 行),每个单元格中有一个按钮。所有按钮都有动态的,因此有不同的标签 ID。在点击事件函数中,我想更改按钮并做一些其他的事情。

如果我使用按钮发送器来识别按钮,例如更改颜色也会更改列表中的另一个按钮。

似乎发件人在滚动时发生了变化。很奇怪。

对不起,我太笨了。我认为我缺少一些明显的东西。

func followButtonTapped(sender: UIButton) {
    println(sender)
    println("UserID: \(sender.tag)")
    sender.enabled = false
    sender.backgroundColor = UIColor.grayColor()
    sender.setTitle("...", forState: UIControlState.Normal)
}

这里是一个示例发件人:

<UIButton: 0x7fe5249bacd0; frame = (297 17; 63 24); opaque = NO; autoresize = RM+BM; tag = 1147; layer = <CALayer: 0x7fe5249c2b10>>
UserID: 1147

这是我的 cellForRowAtIndexPath

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    tableView.tableFooterView = UIView(frame: CGRectZero)
    tableView.estimatedRowHeight = 58

    var cell : followCell! = tableView.dequeueReusableCellWithIdentifier(followCellIdentifier) as followCell!

    if(cell == nil){
        cell = NSBundle.mainBundle().loadNibNamed(followCellIdentifier, owner: self, options: nil)[0] as followCell;
    }

    cell?.followName?.text=self.maintext[indexPath.row]
    cell?.followSubtext?.text = self.subtext[indexPath.row]
    cell?.followButton?.addTarget(self, action: "followButtonTapped:", forControlEvents: .TouchUpInside)
    cell?.followButton?.tag = self.UserIds[indexPath.row].toInt()!

    var image = UIImage(named: "default_avatar_40.jpg")

    var imgURL: NSURL = NSURL(string: self.images[indexPath.row])!
    let request: NSURLRequest = NSURLRequest(URL: imgURL)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
        if error == nil {
            var image = UIImage(data: data)

            if let cellToUpdate = tableView.cellForRowAtIndexPath(indexPath) as? followCell {
                cellToUpdate.followImage.image = image
            }
        }
    })

    cell?.backgroundColor = UIColor.clearColor()

    return cell as followCell
}

【问题讨论】:

    标签: ios swift uibutton xcode6


    【解决方案1】:

    我相信您观察到的行为是由于重用了 tableView 单元格。当您触摸一个按钮然后更改其颜色时,包含该按钮的单元格仅与该表格行相关联,直到它滚出屏幕。单元格(和按钮)将被重新用于屏幕上的另一行。如果您没有在 cellForRowAtIndexPath 中重新设置颜色,屏幕上出现的单元格可能会获得一个已被选中的使用按钮。

    要完成这项工作,您需要做 3 件事:

    1. 独立于按钮,您需要跟踪模型中按下了哪些按钮。例如,在您的视图控制器类中,有一组布尔值,表中的每个按钮都有一个条目。

      var selected:[Bool] = Array(count: 100, repeatedValue: false)
      
    2. cellForRowAtIndexPath 中,根据模型中的布尔数组设置按钮。

      要知道一个按钮关联到哪一行,可以在cellForRowAtIndexPath中设置按钮的标签为indexPath.row,然后访问followButtonTapped中的sender.tag

      cell.button.tag = indexPath.row
      if selected[indexPath.row] {
          cell.button.enabled = false
          cell.button.backgroundColor = UIColor.grayColor()
      } else {
          cell.button.enabled = true
          cell.button.backgroundColor = UIColor.whiteColor()
      }
      
    3. followButtonTapped 中更改数组中与您选择的按钮相对应的布尔值。

      func followButtonTapped(sender: UIButton) {
          let row = sender.tag
          selected[row] = true
          println(sender)
          println("UserID: \(sender.tag)")
          sender.enabled = false
          sender.backgroundColor = UIColor.grayColor()
          sender.setTitle("...", forState: UIControlState.Normal)
      }
      

    【讨论】:

    • 感谢@vacawama 的快速回复,但我不明白这项工作。有没有办法操纵单元标识符?我刚刚添加了我的 cellForRowAtIndexPath
    • 您的表格可能有 100 行,但只会分配大约 8 个单元格。当它们滚动到屏幕外时,它们被放入重用队列并被重用。您不应该依赖单元格来保存任何状态数据。任何状态都应保存在表格单元格之外。这就是我试图用 selected 数组提出的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多