1
ユーザが提供するデータに応じて、自分のUITableViewセルにコンテンツを動的に設定します。たとえば、ユーザーが画像を提供しない場合、UIImageViewはセルを短く表示するために消え去ります。私の問題は、私のセルの内容を動的に処理することによって、再使用されたセルが乱れてしまっていると思います。UITableViewでデータをリロードした後、私のUIImageViewがどうして消えてしまいましたか?
私はスーパービューからイメージビューを削除していますが、テーブルビューをリロードしたときにリセットしないでください。それとも何とかスーパービューに戻す必要がありますか?私が行く前に、不必要にプログラムで複数の制約を追加すると、これを処理する適切な方法は何ですか?私のセルの関連コードは次のとおりです。
func configureItemCell(postTitle: String, postTime: String, postDescription: String, postImageURL: String) {
titleLabel.text = postTitle
timePostedLabel.text = "\(postTime) by"
if postImageURL != "" {
postImageView.sd_setImage(with: URL(string: postImageURL), placeholderImage: UIImage(named: "placeholder"))
} else {
// Remove image view since none was specified
postImageView.removeFromSuperview()
// Attach constraint for description since image is now gone
let descriptionBottomConstraint = NSLayoutConstraint(item: descriptionLabel, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: containerView, attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, constant: -12)
NSLayoutConstraint.activate([descriptionBottomConstraint])
}
if postDescription == "" {
// Shrink distance to 0 since description not showing
descriptionTopConstraint.constant = 0
}
}