Ios 我在UITableView上重新加载数据后,UIImageView怎么会消失?
我根据用户提供的数据动态设置UITableView单元格中的内容。例如,如果用户不提供图像,UIImageView将消失以使单元格看起来更短。我的问题是,我认为对单元格内容的动态处理会导致重复使用的单元格变得混乱 我确信问题是因为我正在从superview中删除图像视图,但是当我重新加载我的表视图时它不应该重置吗?还是我必须以某种方式将其添加回superview?在我开始之前,以不必要的编程方式添加一系列约束,正确的处理方法是什么?这是我手机的相关代码Ios 我在UITableView上重新加载数据后,UIImageView怎么会消失?,ios,swift,uitableview,nslayoutconstraint,Ios,Swift,Uitableview,Nslayoutconstraint,我根据用户提供的数据动态设置UITableView单元格中的内容。例如,如果用户不提供图像,UIImageView将消失以使单元格看起来更短。我的问题是,我认为对单元格内容的动态处理会导致重复使用的单元格变得混乱 我确信问题是因为我正在从superview中删除图像视图,但是当我重新加载我的表视图时它不应该重置吗?还是我必须以某种方式将其添加回superview?在我开始之前,以不必要的编程方式添加一系列约束,正确的处理方法是什么?这是我手机的相关代码 func configureIte
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
}
}
简单的解决方案是使用两种不同的单元格类型。一个是普通的,另一个是特殊的
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
switch item.type {
case .normal:
if let cell = tableView.dequeueReusableCell(withIdentifier: "Normal", for: indexPath) as? NormalCell {
cell.item = item
return cell
}
case .special:
if let cell = tableView.dequeueReusableCell(withIdentifier: "Special", for: indexPath) as? AboutCell {
cell.item = item
return cell
}
简单的解决方案是使用两种不同的单元格类型。一个是普通的,另一个是特殊的
override func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let item = items[indexPath.section]
switch item.type {
case .normal:
if let cell = tableView.dequeueReusableCell(withIdentifier: "Normal", for: indexPath) as? NormalCell {
cell.item = item
return cell
}
case .special:
if let cell = tableView.dequeueReusableCell(withIdentifier: "Special", for: indexPath) as? AboutCell {
cell.item = item
return cell
}