【发布时间】:2017-07-21 13:27:33
【问题描述】:
我实现了一个自定义 UITableView 单元格。
一切都很好,直到我点击了我的表格视图单元格。
当我点击它时,通常一个表格视图单元格会变暗。
这是因为 cell.selectionStyle 设置为 .default,并且 UITableViewCell 的子类通过调用 setHighlighted(_ highlight: Bool, animated: Bool) 使自身变暗。
我的问题是,在我的表格中选择一行(单元格)时,除了 UITableViewCell 内容视图中包含的 UIImageView 之外的所有内容都会正确变暗。
我尝试在我的 UIImgeView 顶部添加一个黑色 UIView 叠加层,并覆盖我的自定义 tableview 单元格的 setHighlighted 以改变 UIView 的不透明度以匹配其余单元格内容。然而,这种影响不仅每次都无法可靠地触发,而且它也不会与其他单元组件携带相同的动画。覆盖层的不透明度变化是即时的,而 iOS 提供的默认调光效果是渐进的。
我真的很感激任何见解。我想这是许多其他人可能遇到的问题,但谷歌搜索没有产生任何结果。谢谢你。以防万一,我在下面包含了我的自定义 UITableViewCell 的代码。
import UIKit
class HouseTableViewCell: UITableViewCell {
var houseThumbnail: UIImageView!
var houseName: UILabel!
var houseRating: UILabel!
var houseReviewCount: UILabel!
var houseRent: UILabel!
var imageDim: UIView!
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: UITableViewCellStyle.value1, reuseIdentifier: reuseIdentifier)
houseThumbnail = UIImageView()
self.contentView.addSubview(houseThumbnail)
houseName = UILabel()
self.contentView.addSubview(houseName)
houseRating = UILabel()
self.contentView.addSubview(houseRating)
houseReviewCount = UILabel()
self.contentView.addSubview(houseReviewCount)
houseRent = UILabel()
self.contentView.addSubview(houseRent)
imageDim = UIView()
imageDim.backgroundColor = UIColor.black.withAlphaComponent(0.0)
self.contentView.addSubview(imageDim)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func makeConstraints() {
houseThumbnail?.snp.makeConstraints { (make) -> Void in
make.width.equalToSuperview().dividedBy(2)
make.height.equalToSuperview()
make.left.equalToSuperview()
}
houseName?.snp.makeConstraints { (make) -> Void in
make.width.equalToSuperview().dividedBy(2)
make.height.equalTo(30)
make.right.equalToSuperview()
make.top.equalToSuperview()
}
houseRating.snp.makeConstraints{ (make) -> Void in
make.height.equalTo(30)
make.top.equalTo(houseName.snp.bottom)
make.left.equalTo(self.snp.centerX)
}
houseReviewCount.snp.makeConstraints{ (make) -> Void in
make.height.equalTo(30)
make.top.equalTo(houseName.snp.bottom)
make.right.equalToSuperview()
}
houseRent.snp.makeConstraints{ (make) -> Void in
make.width.equalToSuperview().dividedBy(2)
make.height.equalTo(30)
make.right.equalToSuperview()
make.bottom.equalToSuperview()
}
imageDim.snp.makeConstraints{ (make) -> Void in
make.width.height.top.left.equalTo(houseThumbnail)
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
super.setSelected(highlighted, animated: animated)
/* Logic to dim imageview */
if highlighted {
imageDim.backgroundColor = UIColor.black.withAlphaComponent(0.2)
} else {
imageDim.backgroundColor = UIColor.black.withAlphaComponent(0.0)
}
}
}
【问题讨论】:
标签: ios uitableview uiimageview