【问题标题】:tableView with custom cells and delay具有自定义单元格和延迟的 tableView
【发布时间】:2019-05-29 11:39:06
【问题描述】:

当我第一次切换到有延迟时,我有一个带有自定义单元格的tableView,在搜索问题后,我发现显示照片是我寻找问题的原因,但没有任何帮助。我试图在主队列中进行转换

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        guard let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? TVCellMenu else { return UITableViewCell()}
        let data = category[indexPath.row]
        cell.title.text = data.name

        //image is reason

        let image = UIImage(named: data.image_name ?? "", in: Bundle.main, compatibleWith: nil)
        cell.ImageView.image = image

        return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let vc = storyBoard.instantiateViewController(withIdentifier: "menu") as! VCMenu
        vc.isCategory = false
        vc.subCategory = self.category[indexPath.row].subCategory
        vc.position = self.category[indexPath.row].position
        navigationController?.pushViewController(vc, animated: true)

}

【问题讨论】:

  • 主队列中的哪个转换?在 didSelectRowAt 中?
  • @Sh_Khan 在 didSelectRowAt 中是的

标签: swift image uitableview tableview


【解决方案1】:

当加载图片很慢时,你应该考虑异步加载它。

例如:

向 TVCellMenu 添加一个函数:

func loadImage(imageName: String) {
    DispatchQueue.main.async {
        let image = UIImage(named: imageName ?? "", in: Bundle.main, compatibleWith: nil)
        self.ImageView.image = image
    }
}

然后(而不是直接设置图像)只需调用 cellForRowAt 中的函数:

    cell.loadImage(imageName: data.image_name ?? ""); 

    return cell

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多