Мое приложение загружает сообщения с моего веб-сервера.Сообщения могут иметь одно или несколько вложений (изображений).
У моего приложения есть TableView
с ячейками.
В каждой ячейке есть StackView
, который должен содержать все вложения, но яне получается заставить его работать.
После прокрутки вниз, затем вверх изображения исчезают.
Что я делаю не так?
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch indexPath.section {
case 0:
let post = self.posts[index]
let cell = tableView.dequeueReusableCell(withIdentifier: "PostViewCell", for: indexPath) as! PostViewCell
cell.messageLabel.text = post.message
cell.userNameButton.setTitle(post.userName, for: .normal)
let date = Date(timeIntervalSince1970: Double(post.leftTime))
dateFormatter.locale = Locale(identifier: "en_US")
var strDate = "";
if (calendar.date(byAdding: .month, value: -1, to: Date())! < date){
strDate = date.relativeTime
} else if (calendar.date(byAdding: .month, value: -12, to: Date())! < date){
let dateFormat = "MMM dd";
dateFormatter.dateFormat = dateFormat
strDate = dateFormatter.string(from: date)
} else {
let dateFormat = "yyyy MMM dd";
dateFormatter.dateFormat = dateFormat
strDate = dateFormatter.string(from: date)
}
if post.attachments.count > 0 {
cell.attachmentsPreviewStackView.isHidden = false
// If cell is reused, remove all older attachments
cell.attachmentsPreviewStackView.subviews.forEach({ $0.removeFromSuperview() })
for attachment in post.attachments {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFill
imageView.image = UIImage(named: "placheholder_thumbnail")
cell.attachmentsPreviewStackView.addArrangedSubview(imageView)
imageView.sd_setImage(with: URL(string: appSettings.url + "/resources/img/attachments/" + attachment.fileName), placeholderImage: UIImage(named: "placeholder_thumbnail"), options: [.avoidAutoSetImage, .progressiveDownload]) { (image, error, type, url) in
if let _ = error {
// placeholder
imageView.image = UIImage(named: "placeholder_thumbnailNotFound")
return
}
DispatchQueue.main.async {
imageView.image = image
}
}
}
} else {
cell.attachmentsPreviewStackView.isHidden = true
cell.attachmentsPreviewStackView.subviews.forEach({ $0.removeFromSuperview() })
}
cell.timeLabel.text = ("• \(strDate)")
if (post.profilePicture != ""){
cell.profilePictureImageView.sd_setImage(with: URL(string: appSettings.url + "/resources/img/pp/" + post.profilePicture), placeholderImage: UIImage(named: "defaultProfilePicture"), options: [.progressiveDownload])
} else {
cell.profilePictureImageView.image = UIImage(named: "defaultProfilePicture")
}
if (post.liked == false){
cell.likeButton.setImage(UIImage(named: "btn_like"), for: .normal)
} else {
cell.likeButton.setImage(UIImage(named: "btn_liked"), for: .normal)
}
cell.commentsButton.setTitle("\(post.comments) comments", for: .normal)
cell.likesButton.setTitle("\(post.likes) likes", for: .normal)
cell.isUserInteractionEnabled = true
cell.delegate = self
cell.delegate2 = self
return cell
case 1:
...
...
}
}