- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
伙计们,我曾经遇到过 Massive View Controller
问题,特别是在使用 UITableView
时,现在我正在将一个旧应用程序更改为更具模式性。以前,我曾经将整个单元格设置在 cellForRowAt indexPath
处。设置标签、下载图片等
现在我正在使用正确的方法在 UITableViewCell
子类中进行设置。但我遇到的问题是,在更新/下载新图片时重用保留旧图片的单元格,我想我已经解决了,但我也注意到它恰好有点把错误的图片放在错误的单元格中,然后在进行快速滚动时更新为正确的...
在我可以做这样的事情以确保图像在正确的单元格中更新之前。那就是像这样从 View Controller 设置整个单元格的时候......
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier")
cell.title = "Cool title"
imageLoader.obtainImageWithPath(imagePath: viewModel.image) { (image) in
// Before assigning the image, check whether the current cell is visible for ensuring that it's right cell
if let updateCell = tableView.cellForRow(at: indexPath) {
updateCell.imageView.image = image
}
}
return cell
}
现在我几乎只在 viewcontroller
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : RestCell = self.tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RestCell
// Pass data to Cell :) clean the mess at the View Controller ;)
cell.restData = items[indexPath.row]
return cell
}
还有 UITableViewCell 子类,我有这个。
class RestCell: UITableViewCell {
@IBOutlet var img : UIImageView!
@IBOutlet var lbl : UILabel!
@IBOutlet var lbl_tipocomida: UILabel!
@IBOutlet var lbl_provincia: UILabel!
@IBOutlet var img_orderonline: UIImageView!
@IBOutlet var img_open: UIImageView!
@IBOutlet var imgalldelivery: UIImageView!
@IBOutlet var img_reservasonline: UIImageView!
// get data and update.
var restData: RestsModel! {
didSet {
// default img early placeholder set. Eliminates showing old pic in cell reuse
self.img.image = UIImage(named: "60x60placeholder.png")
// Update all values func
updateUI()
}
}
override func awakeFromNib() {
super.awakeFromNib()
// Default image placeholder
//self.img.image = UIImage(named: "60x60placeholder.png")
}
// required init.
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
// Color of selected.
let myCustomSelectionColorView = UIView()
myCustomSelectionColorView.backgroundColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 0.1) //UIColor(netHex:0xFFFF434)
selectedBackgroundView = myCustomSelectionColorView
}
fileprivate func updateUI() {
if (self.restData.image != nil && self.restData.image != "") {
ImageLoader.sharedLoader.imageForUrl(urlString:self.restData.image, completionHandler:{(image: UIImage?, url: String) in
self.img.contentMode = UIViewContentMode.scaleAspectFit
self.img.image = image
})
} else {
self.img.image = UIImage(named: "60x60placeholder.png")
}
self.lbl.text = restData.name as String
self.lbl_provincia.text = restData.provincia as String
self.lbl_tipocomida.text = restData.tipocomida as String
// check if has alldelivery enabled, show Alldelivery logo
if( self.restData.reservaonline == "1") {
self.img_reservasonline.isHidden = false
self.img_reservasonline.image = UIImage(named: "icon_reserva_on")
} else {
self.img_reservasonline.isHidden = true
self.img_reservasonline.image = UIImage(named: "icon_reserva_off")
}
// check if has orderonline, show ordeonline text
if ( self.restData.orderonline == "1") {
self.img_orderonline.isHidden = false
} else {
self.img_orderonline.isHidden = true
}
// check if has schedule, display if open or close
if (self.restData.hasSchedule == true) {
if ( self.restData.openNow == true) {
self.img_open.image = UIImage(named:"icon_open")
} else {
self.img_open.image = UIImage(named:"icon_close")
}
self.img_open.isHidden = false
} else {
self.img_open.isHidden = true
}
}
}
我正在从 UITableViewVCell
子类调用图像下载并将其设置在那里。如何确保图像仅设置为正确的单元格???
最佳答案
我建议您使用第 3 方库来加载带有 url 的图像,例如 Kingfisher
。它将为您处理缓存和取消加载。
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let data = dataSource[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Identifier") as! ACell
cell.update(data)
return cell
}
单元格看起来像这样。
import Kingfisher
class ACell: UITableViewCell {
@IBOutlet fileprivate weak var backgroundImageView: UIImageView!
fileprivate func reset() {
backgroundImageView.image = nil
}
func update(_ data: ATypeOfData) {
reset()
let resource = ImageResource(downloadURL: url)
backgroundImageView.kf.setImage(with: resource)
}
}
您失败的原因是您误解了重用单元格。
let updateCell = tableView.cellForRow(at: indexPath)
updateCell
可以是与您要使用的单元格不同的单元格。它可以只是一个重复使用的随机单元格。如果你真的想跟踪一个单元格,你需要设置一个属性并检查 cellForRowAt
中的属性是否与图像加载闭包中的属性相同。我真的建议你使用我附加的模式。
关于swift - UITableViewCell 子类单元格中的错误图像或旧图像错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46901059/
我是 Swift 开发新手,正在学习许多在线教程。这些教程中的大多数都引用了 Xcode 上的旧版本,并且代码会导致错误。谁能解释为什么下面的代码会产生“UITableViewCell”?无法转换为
我在我的单元格中使用 AutoLayout,但是,当单元格内有大文本并且单元格展开时,单元格似乎与下面的单元格重叠。图像显示了这是如何重叠单元格的: UITableViewCell:
我的函数接受类型为 (cell: UITableViewCell, item: AnyObject) -> () 的函数作为参数。 我的问题是,当我尝试将具有 UITableViewCell 的子类
我正在尝试做一些相当简单的事情,但我看不到最好的方法。我有一个带有两个单元格的 uitableview,第一个在 contextview 中有一个 uitextfield,输入 View 设置为 ui
我在使用 Swift 时遇到了一些挑战。 我有一个带有原型(prototype)单元的 UITableView。有3个单元格前两个包含用户填写的文本字段。第三个包含一个提交按钮。 非常简单! 我的问题
我是 Objective C 的新手。我有一个问题。 我使用自定义的tableviewcell。我正确地使用 JSON 列出了数据。 我想将点击的数据传递到新 View 的自定义表格 View 。 所
我有两个单元格,比方说我的应用程序中的单元格 A 和单元格 B。它们都有自己的类,class CellA : UITableViewCell 和 class CellB : UITableViewCe
我有一个 UIViewController,它根据屏幕上的所需选项实例化一些 UITableViewCell。这些选项之一是“说明”,我创建了一个 .XIB 文件来在屏幕上显示说明。但是,在此屏幕上,
我正在尝试创建一个 tableview,其中有一个 uitableviewcell(cell1) 的子类,其中包含一个按钮等。当在 cell1 中单击该按钮时,该按钮应该在其正下方添加和删除 uita
在使用 UITableView 编码时,我遇到了这个我无法理解的错误。我的表格有 3 行,每行设置 50 像素,但第二行除外,我将其设置为 500 像素,它填满整个屏幕。当我向下滚动到底部时,问题出现
UITableViewCell 反射(reflect)两种不同的状态:突出显示和选定。 对我来说,它们听起来相同,那么到底有什么区别? 最佳答案 突出显示发生在触地时。 选定发生在触摸时,然后调用di
我刚刚添加了屏幕截图。它是定制单元吗?正如我所见,“ map ”是右对齐的,“1800”是左对齐的。我该如何创建它? 最佳答案 它确实看起来像一个自定义单元格。因为 UITableViewCell 继
我不太了解 tableview 的整体行为: 我有一个带有我在 Storyboard中定义的动态单元格(reuseIdentifier:单元格)的表格 View 。除此之外,我还有两个使用两个 nib
我有一个带有以下代码的 UITableViewCell,其中用户单击 UITableViewCell 上的“保存”按钮。单元格中的图像已保存,“保存”按钮动画消失,但单元格仍然存在。 -(UIT
我有一个UITableView,每个单元格都包含一个UIImageView 和一个UIButton。点击按钮后我启动相机,我正在尝试更新该单元格索引路径中的 UIImageView 。到目前为止,这是
有谁知道如何在进入编辑模式时从分组的 UITableView 中隐藏多个单元格?当退出编辑模式时,我希望这些行以动画效果隐藏,就像在“联系人”应用程序中看到的那样。 如您所知,在联系人编辑模式下,行数
我在 UITableViewCell 中有一个 subview ,其中包含一个 UILabel 和一个 UIButton。当我按下按钮时,它出于某种原因取消选择 UITableViewCell。 有没
我一直在经历在 UItableViewCell 中设置 UIDatePicker 的过程,当选择其上方的 UITableViewCell 时会显示该 UIDatePicker,然后在选择 UITabl
我必须集成从代码中以编程方式生成的 UITableViewCell,如下所示: UITableViewCell *newCell = [[UITableViewCell alloc] initWith
我现在正在学习 iOS 开发中的 UITableView 类,我想在导航到多个 UITableViewCells 的部分中实现点击一个 UITableViewCell,该附件类型用于复选标记,我应该怎
我是一名优秀的程序员,十分优秀!