【发布时间】: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
}
【问题讨论】: