简体   繁体   English

Xcode6 swift UIButton发送者不唯一吗?

[英]Xcode6 swift UIButton sender not unique?

I'm just wondering why the sender of the UIButton isn't unique or changing. 我只是想知道为什么UIButton的发送者不是唯一的或正在更改。 I have a dynamic filled tableview (about 100 rows) with a button in each cell. 我有一个动态填充的表格视图(约100行),每个单元格中都有一个按钮。 All buttons have dynamic and therefore different tag id's. 所有按钮都有动态的,因此具有不同的标签ID。 In the click event function I'd like to change the button and do some other stuff. 在点击事件功能中,我想更改按钮并做其他一些事情。

If I use the button sender to identify the button for eg changing the color it changes also one other button in the list. 如果我使用按钮发送器来标识按钮(例如更改颜色),它也会更改列表中的另一个按钮。

It seems that the sender are changing while scrolling. 似乎发件人在滚动时正在更改。 All weird. 真奇怪

Sorry for being stupid. 对不起,很抱歉。 I assume there is some obvious stuff I'm missing. 我认为我缺少一些明显的东西。

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

Here an example sender: 这是一个示例发件人:

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

Here my cellForRowAtIndexPath 这是我的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
}

I believe the behavior you are observing is due to the reuse of tableView cells. 我相信您正在观察的行为是由于tableView单元的重用。 When you touch a button and then change its color, the cell that contains that button is only associated with that table row until it scrolls off screen. 当您触摸一个按钮然后更改其颜色时,包含该按钮的单元格仅与该表行相关联,直到它滚动到屏幕之外。 The cell (and button) will be reused for another row that is coming on screen. 该单元格(和按钮)将重新用于屏幕上出现的另一行。 If you don't set the color back in cellForRowAtIndexPath the cell coming on screen might get a used button that has been selected. 如果未在cellForRowAtIndexPath重新设置颜色,则出现在屏幕上的单元格可能会显示一个已使用的按钮。

To make this work, you need to do 3 things: 要使其工作,您需要做三件事:

  1. Independent of the button, you need to keep track of which buttons have been pressed in your model. 与按钮无关,您需要跟踪模型中已按下的按钮。 For instance, in your view controller class, have an array of booleans that has one entry for each button in your table. 例如,在视图控制器类中,具有一个布尔数组,该布尔数组为表中的每个按钮具有一个条目。

     var selected:[Bool] = Array(count: 100, repeatedValue: false) 
  2. In cellForRowAtIndexPath , set up the button based upon your array of booleans in your model. cellForRowAtIndexPath ,根据模型中的布尔数组设置按钮。

    To know which row a button is associated with, you can set the button's tag to indexPath.row in cellForRowAtIndexPath and then access the sender.tag in followButtonTapped . 要知道按钮与哪一行相关,可以在cellForRowAtIndexPath中将按钮的标记设置为indexPath.row ,然后在sender.tag中访问followButtonTapped

     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. In followButtonTapped change the boolean in the array that corresponds to the button you are selecting. 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) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM