簡體   English   中英

如何在 Swift 中完成所有單元格數據加載之前不顯示 tableView

[英]How to not show tableView until all cell data is finished loading in Swift

我有一個 tableView,但是當它加載單元格時,它看起來很難看,因為圖像需要一兩秒鍾才能加載。我有一個要顯示的加載器,然后當所有單元格都加載完畢后,我想顯示 tableView 和隱藏裝載機。 如何判斷 tableView 何時完成加載並顯示 tableView?

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

    if payment.message == "none" {
        cell.detailsLabel.text = "No Message"
    } else {
        cell.detailsLabel.text = "\"\(payment.message ?? "")\""
    }

        cell.amountLabel.textColor = UIColor.init(red: 105/255, green: 105/255, blue: 105/255, alpha: 1)
        cell.amountLabel.text = "$\(payment.amount ?? "")"
          
        return cell
    }

所以基本上你想顯示Activity Indicator來警告/顯示你的用戶等待一段時間,直到可見單元格正確加載,對嗎?

你有你的自定義加載器( Activity Indicator ),你想在所有單元格都按你寫的加載后顯示。

這里需要理解的重要一件事是UITableView不會加載所有單元,因為它不能那樣工作。

無論您有多少項目,表格視圖都只會使用tableView:cellForRowAtIndexPath:創建 6 或 7 個可見的單元格(取決於)。

如果用戶滾動,則將再次調用上述方法以獲取新的可見單元格。

所以這意味着

該表從未真正完成加載。

回答有關如何檢測表格是否完成加載可見單元格的問題,那么您可以嘗試這段代碼:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let lastVisibleIndexPath = tableView.indexPathsForVisibleRows?.last {
                if indexPath == lastVisibleIndexPath {
                    // turn off/hide your loader
                }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM