使用UIImages填充TableViewCell的StackView

时间:2019-05-24 19:31:37

标签: swift uitableview uiimage stackview

我的应用程序从我的Web服务器下载帖子。这些帖子可能包含一个或多个附件(图片)。

我的应用中有一个带有单元格的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:
            ...
        ... 
        }
    }

0 个答案:

没有答案