我有點卡住了。我有一個自定義UIView,其中包含一個計時器和一個UIImageView,該計時器以恒定間隔觸發并更改影像陣列中的影像。當我將它嵌入到任何其他UIView. 它以規定的速度更改影像并且沒有任何問題。它按我的意愿作業。
然而,這個完全相同的視圖,當放置在UITableVieCell如下所示的 a 中時,只會渲染第一張影像并且不會改變。我已經確認計時器正在觸發,并且影像視圖正在通過列印對UIImageView.image. 也就是說,當嵌入到 中時UITableViewCell,我的自定義UIView正在觸發它的計時器,而UIImageView在這個自定義視圖中,認為它正在改變它的影像。這讓我相信這是一個顯示問題而不是資料設定問題。
結果,這是嘗試過的:
- 我試過把它包起來
DispatchQueue.main.async{} - 添加一個委托,以便我
UIViewController可以tableView.reloadData()在計時器觸發時呼叫 - 洗掉
multiImageView.images我的二傳手prepareForReuse
下面的自定義單元格
import UIKit
import SnapKit
final internal class MultiImageCell: UITableViewCell {
// MARK: - UI Elements
public var multiImageView: MultiPartImageView = MultiPartImageView()
// MARK: - Lifecycle
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: reuseIdentifier)
//Setup Cell
selectionStyle = .none
//Setup Section Title View
multiImageView.rotationSpeed = 0.3
contentView.addSubview(multiImageView)
//Setup Constraints
setupConstraints()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func prepareForReuse() {
super.prepareForReuse()
accessoryType = .none
multiImageView.images = []
}
override func layoutSubviews() {
super.layoutSubviews()
setupConstraints()
}
// MARK: - Functions
private func setupConstraints() {
//Setup Section View Constraints
multiImageView.snp.remakeConstraints({ (make) in
make.edges.equalToSuperview().inset(16)
make.width.height.equalTo(240)
})
}
}
在單元格上設定影像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Check for Cell
guard let row = viewModel.row(at: indexPath) else {
return UITableViewCell()
}
// Setup Cell
let cell = tableView.dequeueReusableCell(withIdentifier: row.cellReuseIdentifier,
for: indexPath)
cell.textLabel?.text = viewModel.title(for: row, indexPath: indexPath)
cell.detailTextLabel?.text = row.detailText
cell.accessoryView = row.accessoryView
cell.accessoryType = row.accessoryType ?? .none
cell.imageView?.image = row.image
switch RowStyle(rawValue: row.cellReuseIdentifier) {
case .multiImageCell:
guard let multiImageCell: MultiImageCell = cell as? MultiImageCell else {
return cell
}
guard let multiImageRow: MultiImageRow = row as? MultiImageRow else {
return cell
}
multiImageCell.multiImageView.images = multiImageRow.images
default:
break
}
return cell
}
uj5u.com熱心網友回復:
看來這是 M1 處理器上 iOS 15 模擬器上的一個錯誤。代碼在真實設備上運行完美,但在模擬器上運行不正常。我打算為此提交雷達。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/363807.html
上一篇:RXSwift在系結資料時一直警告我tableView.rx.items(dataSource)不符合RxTableViewDataSourceType
