【发布时间】:2015-09-25 02:19:39
【问题描述】:
我有一个带有 CustomCollectionViewCell 类的 UICollectionView。 在单元格内,我有一个使用情节提要设置的按钮。我希望按钮在点击时更改其颜色,但我一直在选择的标题的阴影之类的情况下,在选择时的背景下的背景。
我尝试用setTitleShadowColor:ForState 和.Selected 和.Normal 修复它。但它似乎没有做任何事情。
视图控制器:
...
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return numberOfButtons
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CustomCollectionViewCell
cell.setContents(indexPath.row)
return cell
}
...
CustomCollectionViewCell:
...
func setContents(row: Int) {
buttonInCell.setTitle(String(row + 1), forState: .Normal)
buttonInCell.setTitleColor(UIColor.blueColor(), forState: .Normal)
buttonInCell.layer.backgroundColor = UIColor.whiteColor().CGColor
buttonInCell.layer.cornerRadius = buttonRadius
buttonInCell.layer.borderColor = UIColor.blueColor().CGColor
buttonInCell.layer.borderWidth = buttonBorderWidth
}
func changeButtonStatus(sender: UIButton) {
if sender.selected {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.whiteColor().CGColor
} else {
sender.setTitleColor(UIColor.blueColor(), forState: .Normal)
sender.layer.backgroundColor = UIColor.blueColor().CGColor
}
}
// MARK: Actions
@IBAction func buttonInCellTapped(sender: UIButton) {
changeButtonStatus(sender)
sender.selected = !sender.selected
}
...
【问题讨论】: