2017-07-21 6 views
0

カスタムUITableViewセルを実装しました。UIImageViewをカスタムUITableViewCellで調整する選択した場合

テーブルビューのセルをクリックするまで、すべてが良好でした。

通常、テーブルビューセルはタップすると薄暗くなります。

これは、cell.selectionStyleが.defaultに設定されているため、setHighlighted(_ highlighted:Bool、animated:Bool)の呼び出しでUITableViewCellのサブクラス自体が暗くなるためです。

私の問題は、私のテーブルで行(セル)を選択すると、UITableViewCell contentviewに含まれている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) 
    } 
} 

}

答えて

0

私は、あなたの問題は私の知る限りでは

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) 
    } 
} 

であると思いのUITableViewCellのセルを選択する際setHighlightedが呼び出されません。 代わりに、

override func setSelected(_ selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    /* Logic to dim imageview */ 
    if selected { 
     imageDim.backgroundColor = UIColor.black.withAlphaComponent(0.2) 
    } else { 
     imageDim.backgroundColor = UIColor.black.withAlphaComponent(0.0) 
    } 
} 
に変更してください
関連する問題