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

标签 ios swift

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

相关文章:

ios - 检测是否在 UITextField 中输入了 "space",并移动到下一个 UITextField

xcode - 如何在表格末尾添加 mapview 作为页脚(Swift)

ios - Swift 服务器实现

ios - WKWebView的标题等于document.title?

objective-c - 如何检测 objective-c 中的外部键盘连接?

swift - 将 UITabBar 移动到底部

ios - 在 SpriteKit 中处理不同的 iOS 设备分辨率

iphone - xib 文件中的 iOS 自定义 View

swift - 无法将类型 'NSTaggedPointerString' (0x10146ecd8) 的值转换为 'NSNumber' (0x102675600)

c++ - 如何制作从 C++ 到 Swift 的回调函数指针?