ios - swift:从本地 json 文件填充的 uitableview 在滚动时严重断断续续

标签 ios json swift uitableview

当按钮被点击以跳转至表格 View 时,它需要大约 5 秒的时间来跳转。在它最终 segues 之后,当 tableview 滚动时,它会断断续续,有时甚至会崩溃。 tableview 从本地 json 文件填充并引用本地镜像。图像被优化为小尺寸。是什么导致了这种情况?我该如何优化/修复我的代码以阻止这种情况发生?

import UIKit

class PDList: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var pdTableView: UITableView!

var pdArt = [Decode]()

override func viewDidLoad() {
    super.viewDidLoad()

    json()

    pdTableView.delegate = self
    pdTableView.dataSource = self

}

func json() {

    let path = Bundle.main.path(forResource: "palmdesert", ofType: "json")
    let url = URL(fileURLWithPath: path!)

    do {
        let data = try Data(contentsOf: url)
        self.pdArt = try JSONDecoder().decode([Decode].self, from: data)
    }
    catch {}

}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return pdArt.count

}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "pdCell")
    let image = UIImage(named: pdArt[indexPath.row].pic)
    cell.textLabel?.text = pdArt[indexPath.row].art
    cell.detailTextLabel?.text = pdArt[indexPath.row].artist.capitalized
    cell.imageView?.image = image 

    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 100
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "pdDetail", sender: self)
    self.pdTableView.deselectRow(at: indexPath, animated: true)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let destination = segue.destination as? PDDetail {
        destination.pdArt = pdArt[(pdTableView.indexPathForSelectedRow?.row)!]
    }
}

@IBAction func done(sender: AnyObject) {
    dismiss(animated: true, completion: nil)
}

}

JSON 示例:

{
  "art": "Agave",
  "desc": "Agave by Michael Watling serves as the City's entry monument and is inspired by the imagery of the agave, a succulent native to the desert.  The stone forms are representative of Palm Desert's many well-managed resources for survival and growth.",
  "artist": "Michael Watling",
  "lat": 33.7215,
  "long": -116.362,
  "pic": "test.png"
  }

时间简介: Time Profile Time Profile2

如果您需要任何其他信息,请告诉我。感谢您的帮助。

最佳答案

我构建了一个示例 UITableView 项目,其中包含 20 张 4032 × 3024 的大型 jpeg 图像的 Assets 库,通过 UIImage(named:) 到我的单元格。

对于 120 x 70 的 ImageView 来说,这大大超出规范,因此最大要求为 360 x 210

时间分析器显示了这一点,用于滚动一批全新的 12 个单元格。

enter image description here

每个单元大约 54 毫秒 (650/12)。然而,即使是大型 JPG 也会利用硬件解码,所以不太不好。

60Hz 刷新率的现代 iWidget 为您提供每帧最多 16 毫秒的单元格刷新时间。所以我口吃。

将图像替换为适当缩放的图像 (360 x 210 ) 即可。

enter image description here

每个单元大约 14 毫秒。表格滚动大部分是平滑的。

所以这里的问题可能是其中之一。

  • 您的图片太大。确保您的资源的像素大小是 ImageView 大小的 3x
  • 您的图片不是 PNG 或 JPEG 格式,无法传递给硬件解码器或使用 pngcrush 进行处理。确保您的图像是 PNG 或 JPEG。例如 GIF 会很糟糕。
  • 当 JPG 可能更合适时,您正在使用 PNG。真实世界的图像最好是 JPG。艺术线条/纯色最好是 PNG。

关于ios - swift:从本地 json 文件填充的 uitableview 在滚动时严重断断续续,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48312813/

相关文章:

ios - 如何在 Xcode 的 UI 测试中检查是否存在从网络显示的静态文本?

javascript - 基于标题搜索使用JavaScript修改JSON对象

javascript - 将字符串从本地存储转换回数组

ios - Swift 认为我错误地继承了 NSSet 但我根本没有继承它

iphone - iOS 动画启动画面

ios - 没有确认提示的自定义 URL 方案 (Swift)

java - Java 中的 JSON 解析器自动将字符串转换为数字/整数

swift 4 : Cannot use instance member 'MAIN_URL' within property initializer when trying to concatenate two strings

swift - 通过 Action 使 SKPhysicsBody 充满活力

ios - OpenGL 与透明背景混合?