gpt4 book ai didi

ios - swift:带有图像卡住的表格 View

转载 作者:搜寻专家 更新时间:2023-11-01 07:09:44 27 4
gpt4 key购买 nike

我的 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
}

更新

  • 创建一个名为 MyCustomCell.swift 的新 swift 文件 enter image description here
  • 在 Storyboard中,将单元格的类设置为 MyCustomCell enter image description here
  • 在您的单元格中添加一个 UIImageView enter image description here
  • 连接@IBOutlet enter image description here
  • 设置您的单元格标识符 enter image description here
  • 更新您的 cellForRow 方法 enter image description here

关于ios - swift:带有图像卡住的表格 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45670429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com