简体   繁体   English

如何隐藏 tableview 直到图像完全加载 iOS Swift?

[英]How to hide tableview until images are completely loaded iOS Swift?

My images take a second to load before they appear, which looks bad.我的图像在出现之前需要一秒钟才能加载,这看起来很糟糕。 On apps such as instagram, the tableview is hidden until the tableview is loaded... how do they do this?在 instagram 等应用程序上,tableview 是隐藏的,直到加载 tableview ......他们是怎么做到的? I have a loader that I want to display but don't know when to stop it and show tableview and detect the images have first finished loading?我有一个要显示的加载器,但不知道何时停止它并显示 tableview 并检测图像是否已首先完成加载? Where do I put stopTimer()?我应该把 stopTimer() 放在哪里?

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",
                                                 for: indexPath) as! MainTableViewCell
    let payment = self.payments[indexPath.row]
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
    cell.profilePicture.clipsToBounds = true

                if let profileImageUrl = payment.picture {
                    cell.profilePicture.loadImageUsingCacheWithUrlString(profileImageUrl)
                    }
                
    if payment.message == "none" {
        cell.detailsLabel.text = "No Message"
    } else {
        cell.detailsLabel.text = "\"\(payment.message ?? "")\""
    }
}

MY CODE TO FETCH IMAGE IN TABLEVIEW:我在 TABLEVIEW 中获取图像的代码:

let imageCache = NSCache<NSString, AnyObject>()

extension UIImageView {

func loadImageUsingCacheWithUrlString(_ urlString: String) {
    self.image = nil
    
    //check cache for image first
    if let cachedImage = imageCache.object(forKey: urlString as NSString) as? UIImage {
        self.image = cachedImage
        return
    }
    
    //otherwise fire off a new download
    guard let url = URL(string: urlString) else { return }
    URLSession.shared.dataTask(with: url, completionHandler: { (data, response, error) in
        
        //download hit an error so lets return out
        if let error = error {
            print(error)
            return
        }
        
        DispatchQueue.main.async(execute: {
            
            if let downloadedImage = UIImage(data: data!) {
                imageCache.setObject(downloadedImage, forKey: urlString as NSString)
                
                self.image = downloadedImage
            }
        })
        
    }).resume()
}
}

在此处输入图像描述

You can simple use SDWebImage with cocoaPods and use it for async image downloader with cache support.您可以简单地将SDWebImage与 cocoaPods 一起使用,并将其用于支持缓存的异步图像下载器。 Your cell will look like after ad SDWebImage.在广告 SDWebImage 之后,您的单元格将看起来像。

import SDWebImage


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell",
                                             for: indexPath) as! MainTableViewCell
    let payment = self.payments[indexPath.row]
    cell.profilePicture.layer.cornerRadius = cell.profilePicture.frame.size.width / 2
    cell.profilePicture.clipsToBounds = true

    if let profileImageUrl = payment.picture {
        cell.profilePicture.sd_setImage(with: profileImageUrl, placeholderImage: UIImage(named: "yourPlaceholderImage.png"))
    }
            
    if payment.message == "none" {
    cell.detailsLabel.text = "No Message"
    } else {
    cell.detailsLabel.text = "\"\(payment.message ?? "")\""
    }
}

There is no need to hide tableView for downloading image.无需隐藏 tableView 即可下载图片。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 iOS 13 Swift 中完全隐藏标题栏而不是导航栏 - How to completely hide title bar but not navigation bar in iOS 13 Swift Swift 3:tableView复制Firebase中加载的图像 - Swift 3 : tableView duplicating loaded images from Firebase Tableview数组未在iOS Swift中正确加载? - Tableview array is not loaded properly in iOS Swift? SDWebImage 不加载图像直到我在 Swift 中滚动 tableView - SDWebImage not loading images Until I scroll the tableView in Swift Swift iOS-如何更改已在屏幕上加载的所有可见单元的TableView单元属性,而无需重新加载数据? - Swift iOS -How to change TableView Cell Properties for All Visible Cells Already Loaded On Screen Without Reloading Data? Swift ios11 NavigationItem SearchBar不会完全隐藏 - Swift ios11 NavigationItem SearchBar won't hide completely 从选择TableView单元格丢失参考加载的ios Swift ViewController - ios Swift ViewController Loaded from Selecting TableView Cell Loses Reference 如何在TableView的每一行中添加按钮,图像和其他UI元素? (iOS,快速) - How to add buttons, images and other UI elements in each row of TableView? (iOS,Swift) 如何使用 SDWebImage 在 tableview Swift 中显示图像? - How to use SDWebImage to show images in tableview Swift? 从Firebase存储器中提取图像时,直到我通过滚动与tableView交互时,图像才加载... SWIFT 3 - When pulling images from a Firebase storage the images don’t load until I interact with the tableView by scrolling… SWIFT 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM