当我滚动tableView时,UIImageView没有显示正确的图像。如何在 swift 4语言中正确完成此操作。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
//Transform Data From ^ to load at the bottom
tableView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.contentView.transform = CGAffineTransform (scaleX: 1,y: -1);
cell?.accessoryView?.transform = CGAffineTransform (scaleX: 1,y: -1);
let username = cell?.viewWithTag(1) as! UITextView
username.text = messageArray[indexPath.row].username
let message = cell?.viewWithTag(2) as! UITextView
message.text = messageArray[indexPath.row].message
let timeStamp = cell?.viewWithTag(3) as! UILabel
timeStamp.text = messageArray[indexPath.row].timeStamp
let imageView = cell?.viewWithTag(4) as! UIImageView
let urlString = messageArray[indexPath.row].photoUrl
imageView.layer.cornerRadius = 10
imageView.clipsToBounds = true
//Load profile image(on cell) with URL & Alamofire Library
let downloadURL = NSURL(string: urlString!)
imageView.af_setImage(withURL: downloadURL! as URL)
return cell!
}
答案 0 :(得分:2)
您的解决方案:由于单元格是可重复使用的,因此每次调用单元格时都需要更新图像。
最佳实践:我认为你可以用更好的方式思考并避免强行展开。
SDWebCache
[https://github.com/rs/SDWebImage]窗格进行图片下载和缓存。Model
方式将数据与单元格绑定。细胞类
import UIKit
import SDWebCache
public struct TableCellModel {
var username: String
var imageURL: String
var messsage: String
var timeStamp: String
}
class TableCell: UITableViewCell {
@IBOutlet weak var username: UITextView!
@IBOutlet weak var message: UITextView!
@IBOutlet weak var timeStamp: UITextView!
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
}
var item: Any? {
didSet {
self.configure(item)
}
}
func configure(_ item: Any?) {
if let model = item as? TableCellModel {
self.username.text = model.username
self.message.text = model.message
self.timeStamp.text = model.timeStamp
if let url = URL(model.imageURL) {
self.imageView.sd_setImageWithURL(url)
}
}
}
}
var cellItems: [TableCellModel] = []
viewDidLoad()
中初始化数组 在视图控制器中:在viewDidLoad()
func setupData() {
self.cellItems = [TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" ),
TableCellModel(username: "ABC", imageURL: "www....", message: "your message", timeStamp: "22.33.2222" )]
self.tableView.reloadData()
}
tableView cellforRow
委托就像
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! TableCell
cell.item = self.cellItems[indexpath.row]
return cell
}
答案 1 :(得分:1)
在cellForRowAtIndexPath方法的开头设置.gitignore
。你正在重复使用细胞。有时您需要重置单元组件。
答案 2 :(得分:0)
我们在这里使用可重复使用的单元格,确实reuses
具有旧图像的单元格。然后,每次在cellForRowAtIndexPath
:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell") as! MenuTableViewCell
//MARK:- Show the image
let imgName = "menu_\(indexPath.row + 1)"
//MARK:- Disable the UITableView selection highlighting
cell.selectionStyle = .none
return cell
}
您可以在Assets.xcassets
中设置您喜欢的图像或任何您想要的格式 -