29,027
社区成员
发帖
与我相关
我的任务
分享
let cityLocationButton = collectionCell!.viewWithTag(1) as! UIButton
获取 Button 的时候,第二个 Cell 的 Button 获取不到,就算把第二个 Button 的 Tag 设置为2也获取不到,请问是怎么回事呢?下面贴上我的代码:
var collectionCell: UICollectionViewCell?
var cellID: String?
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
if indexPath.row > 0{
cellID = "collectionCellCityName"
}else{
cellID = "collectionCellLocation"
}
collectionCell = collectionView.dequeueReusableCellWithReuseIdentifier(cellID!, forIndexPath: indexPath) as UICollectionViewCell
let cityLocationButton = collectionCell!.viewWithTag(1) as! UIButton
if indexPath.row > 0{
cityLocationButton.setBackgroundImage(UIImage(named: "citylocation_city"), forState: UIControlState.Normal)
cityLocationButton.setTitle(cities[indexPath.row-1], forState: UIControlState.Normal)
}
cityLocationButton.tag = indexPath.row
//添加点击事件
cityLocationButton.addTarget(self, action: "clickForCityName:", forControlEvents: UIControlEvents.TouchUpInside)
return collectionCell!
}
import UIKit
class CollectionItemClickEffect : UIButton {
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
setBackgroundImage(UIImage(named: "citylocation_city_touchdown"), forState: UIControlState.Normal)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
UIButton.animateWithDuration(0.15, animations: { () -> Void in
self.setBackgroundImage(UIImage(named: "citylocation_city"), forState: UIControlState.Normal)
})
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIButton.animateWithDuration(0.15, animations: { () -> Void in
self.setBackgroundImage(UIImage(named: "citylocation_city"), forState: UIControlState.Normal)
})
}
}
这个类只定义了 Button的点击效果,为什么会连点击事件都失去了呢?有哪位大大知道问题出在哪么?