我的 Xcode swift 项目中有 uitableview
,其中有 10 行图像。我使用此代码在我的 uitableview
中按行显示 10 张图像:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(format: "Cell0", indexPath.row), for: indexPath)
var imageV = UIImageView()
imageV = cell.viewWithTag(5) as! UIImageView
imageV.image = UIImage(named:"image\(indexPath.row + 1).png")
但是当我滚动我的 uitableview
时非常非常非常困难。为什么会这样?以及如何解决?
最佳答案
创建自定义单元格
class MyCustomCell: UITableViewCell {
@IBOutlet weak var imgView: UIImageView!
}
在您的 Storyboard中,将单元格的类设置为 MyCustomCell
并为其指定一个标识符,例如 cell
。
然后,如下更新您的cellForRow
方法
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MyCustomCell
let imageName = "image\(indexPath.row + 1).png"
cell.imgView.image = UIImage(named: imageName)
return cell
}
更新
关于ios - swift:带有图像卡住的表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670429/