uitableViewCell 滚动时未加载
tableViewCell not loading when scrolling
我知道这个问题是徒劳的,但我已经搜索了所有以前的问题,但没有找到可以提供帮助的解决方案。我在后端和存储中使用 Firebase。当用户上传照片时,它会进入我的 Firebase 存储,并按照教程缓存照片以轻松处理数据。我访问缓存数据的方式是这样的:
let imageCache = NSCache<NSString, UIImage>()
extension UIImageView {
func loadImageUsingCachWithUrlString(urlString: String) {
self.image = nil
if let cachedImage = imageCache.object(forKey: urlString as NSString) as UIImage?{
self.image = cachedImage
return
}
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data,response,error) in
if error != nil{
print(error as Any)
return
}
DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data!){
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
}
}).resume()
}
我通过函数 "loadImageUsingCachWithUrlString"
访问此扩展程序
进入我的 table 视图,我在每个单元格中加载用户姓名年龄生物等,我有 Firebase 持久性(以防万一),我下载的方式用户信息进入单元格是通过这个
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserCell
let cells = tableView.visibleCells
let user = users[indexPath.row]
let profileImageUrlThird = user.profileImageUrl
let tableViewHeight = tableView.bounds.size.height
cell.nameLabel.text = user.name
cell.bioLabel.text = user.aboutMe
cell.reviewRating.text = user.averageRating
cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrlThird!)
let tempNumber = user.profileView
let stringTemp = tempNumber?.stringValue
cell.profileViewCount.text = stringTemp
cell.imageView?.contentMode = .scaleAspectFill
cell.backgroundColor = UIColor.clear
cell.textLabel?.font = UIFont(name:"Avenir", size:22)
tableView.layer.borderWidth = 0;
tableView.layer.borderColor = UIColor.lightGray.cgColor
print(tableView.frame.height,tableView.frame.width)
//tableview height is 670 table view width is 414
print(cell.frame.width,cell.frame.height)
//cell width is 414 cell height is 330
for cell in cells {
cell.transform = CGAffineTransform(translationX: 0, y: tableViewHeight)
}
return 单元格
}
信息加载,一切正常,但只要我拖动,单元格就会消失。任何帮助将不胜感激,我已经坚持了将近一个星期
tableView.dequeueReusableCell()
可能 return 一个 nil
值。结果reuse pool中没有这个cell,需要自己生成一个。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifer = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: identifer)
if cell == nil { // when no reusable cell
cell = UITableViewCell(style: .default, reuseIdentifier: identifer)
}
cell?.textLabel?.text = "row:\(indexPath.row)"
return cell!
}
我知道这个问题是徒劳的,但我已经搜索了所有以前的问题,但没有找到可以提供帮助的解决方案。我在后端和存储中使用 Firebase。当用户上传照片时,它会进入我的 Firebase 存储,并按照教程缓存照片以轻松处理数据。我访问缓存数据的方式是这样的:
let imageCache = NSCache<NSString, UIImage>()
extension UIImageView {
func loadImageUsingCachWithUrlString(urlString: String) {
self.image = nil
if let cachedImage = imageCache.object(forKey: urlString as NSString) as UIImage?{
self.image = cachedImage
return
}
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data,response,error) in
if error != nil{
print(error as Any)
return
}
DispatchQueue.main.async {
if let downloadedImage = UIImage(data: data!){
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
}
}).resume()
}
我通过函数 "loadImageUsingCachWithUrlString"
访问此扩展程序进入我的 table 视图,我在每个单元格中加载用户姓名年龄生物等,我有 Firebase 持久性(以防万一),我下载的方式用户信息进入单元格是通过这个
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! UserCell
let cells = tableView.visibleCells
let user = users[indexPath.row]
let profileImageUrlThird = user.profileImageUrl
let tableViewHeight = tableView.bounds.size.height
cell.nameLabel.text = user.name
cell.bioLabel.text = user.aboutMe
cell.reviewRating.text = user.averageRating
cell.profileImageView.loadImageUsingCachWithUrlString(urlString: profileImageUrlThird!)
let tempNumber = user.profileView
let stringTemp = tempNumber?.stringValue
cell.profileViewCount.text = stringTemp
cell.imageView?.contentMode = .scaleAspectFill
cell.backgroundColor = UIColor.clear
cell.textLabel?.font = UIFont(name:"Avenir", size:22)
tableView.layer.borderWidth = 0;
tableView.layer.borderColor = UIColor.lightGray.cgColor
print(tableView.frame.height,tableView.frame.width)
//tableview height is 670 table view width is 414
print(cell.frame.width,cell.frame.height)
//cell width is 414 cell height is 330
for cell in cells {
cell.transform = CGAffineTransform(translationX: 0, y: tableViewHeight)
}
return 单元格 }
信息加载,一切正常,但只要我拖动,单元格就会消失。任何帮助将不胜感激,我已经坚持了将近一个星期
tableView.dequeueReusableCell()
可能 return 一个 nil
值。结果reuse pool中没有这个cell,需要自己生成一个。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let identifer = "cell"
var cell = tableView.dequeueReusableCell(withIdentifier: identifer)
if cell == nil { // when no reusable cell
cell = UITableViewCell(style: .default, reuseIdentifier: identifer)
}
cell?.textLabel?.text = "row:\(indexPath.row)"
return cell!
}