Commit 20d13a65 authored by Alex朱枝文's avatar Alex朱枝文

资源详情多图片刷新优化

parent 7233a776
...@@ -246,6 +246,19 @@ extension YHResourceDetailViewController: UITableViewDataSource, UITableViewDele ...@@ -246,6 +246,19 @@ extension YHResourceDetailViewController: UITableViewDataSource, UITableViewDele
cell.onImageTapped = { [weak self] index in cell.onImageTapped = { [weak self] index in
self?.showImageBrowser(at: index) self?.showImageBrowser(at: index)
} }
cell.onImageHeightChanged = { [weak self] in
guard let self = self else { return }
// NSObject.cancelPreviousPerformRequests(
// withTarget: self,
// selector: #selector(self.performBatchReload),
// object: nil
// )
// self.perform(#selector(self.performBatchReload), with: indexPath, afterDelay: 0.01)
// // 使用 performBatchUpdates 避免动画跳动
UIView.performWithoutAnimation {
self.tableView.reloadRows(at: [indexPath], with: .none)
}
}
return cell return cell
default: default:
...@@ -253,6 +266,12 @@ extension YHResourceDetailViewController: UITableViewDataSource, UITableViewDele ...@@ -253,6 +266,12 @@ extension YHResourceDetailViewController: UITableViewDataSource, UITableViewDele
} }
} }
@objc private func performBatchReload(indexPath: IndexPath) {
UIView.performWithoutAnimation {
self.tableView.reloadRows(at: [indexPath], with: .none)
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension return UITableView.automaticDimension
} }
......
...@@ -53,6 +53,7 @@ class YHResourceDetailDocumentsCell: UITableViewCell { ...@@ -53,6 +53,7 @@ class YHResourceDetailDocumentsCell: UITableViewCell {
private var images: [String] = [] private var images: [String] = []
var onImageTapped: ((Int) -> Void)? var onImageTapped: ((Int) -> Void)?
var onImageHeightChanged: (() -> Void)?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier) super.init(style: style, reuseIdentifier: reuseIdentifier)
...@@ -71,6 +72,10 @@ class YHResourceDetailDocumentsCell: UITableViewCell { ...@@ -71,6 +72,10 @@ class YHResourceDetailDocumentsCell: UITableViewCell {
contentView.addSubview(titleLabel) contentView.addSubview(titleLabel)
contentView.addSubview(noDataView) contentView.addSubview(noDataView)
contentView.addSubview(imagesStackView) contentView.addSubview(imagesStackView)
titleLabel.setContentCompressionResistancePriority(.required, for: .vertical)
titleLabel.setContentHuggingPriority(.required, for: .vertical)
imagesStackView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
imagesStackView.setContentHuggingPriority(.defaultLow, for: .vertical)
titleLabel.snp.makeConstraints { make in titleLabel.snp.makeConstraints { make in
make.left.equalToSuperview().offset(20) make.left.equalToSuperview().offset(20)
...@@ -137,18 +142,36 @@ class YHResourceDetailDocumentsCell: UITableViewCell { ...@@ -137,18 +142,36 @@ class YHResourceDetailDocumentsCell: UITableViewCell {
imageView.isUserInteractionEnabled = true imageView.isUserInteractionEnabled = true
if let url = URL(string: imageUrl) { if let url = URL(string: imageUrl) {
imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "people_head_default")) // 设置图片视图的高度
imageView.snp.makeConstraints { make in
make.height.equalTo(imageHeight)
}
imageView.sd_setImage(with: url, placeholderImage: UIImage(named: "plan_product_default")) { [weak self] image, _, _, _ in
guard let self = self else {
return
}
guard let image = image, image.size.width > 0, image.size.height > 0 else {
return
}
let newImageHeight = image.size.height / image.size.width * imageWidth
// 关键:用 updateConstraints 而不是 remakeConstraints
imageView.snp.updateConstraints { make in
make.height.equalTo(newImageHeight)
}
self.onImageHeightChanged?()
}
} else {
// 设置图片视图的高度
imageView.snp.makeConstraints { make in
make.height.equalTo(imageWidth)
}
imageView.image = UIImage(named: "plan_product_default")
} }
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:))) let tapGesture = UITapGestureRecognizer(target: self, action: #selector(imageTapped(_:)))
imageView.addGestureRecognizer(tapGesture) imageView.addGestureRecognizer(tapGesture)
imageView.tag = index imageView.tag = index
// 设置图片视图的高度
imageView.snp.makeConstraints { make in
make.height.equalTo(imageHeight)
}
imagesStackView.addArrangedSubview(imageView) imagesStackView.addArrangedSubview(imageView)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment