簡體   English   中英

如何在Swift中減少表格視圖的延遲?

[英]How do I make my table view less laggy in Swift?

我的目標是在表格視圖中為每一行簡單顯示唯一的圖像和標簽。 我目前在這里:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath )-> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DailyBarCellViewTableViewCell

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0)) {
            let data = DailyBarCellViewTableViewCell.Static.data
        dispatch_async(dispatch_get_main_queue()) {
            cell.barName?.text = data.namesArray[indexPath.row]
        }
    }

  return cell
} 

CellDataDisplayInformation的內容:

struct CellDataDisplayInformation {
    var namesArray : [String?] = []
    var pictureArray : [UIImage?] = []

    init(){
        for index in 0...21 {
            var data = BarDetails(index: index)
            if data.barName != nil && data.barImage != nil {

                namesArray.append(data.barName!)
                pictureArray.append(data.barImage!)


            }
        }

    }

  }

data = CellDataDisplayInformation()包含[Strings?]和[UIImages?]的數組,分別在每個單元格中顯示。 通過這種方法,UI最初會在每個單元格中加載所有正確的信息。 但是,當我向下滾動到看不見的單元格時,每個單元格中的標簽會隨機播放2秒鍾,然后移至它們的正確位置。

如果我不使用“調度”,那就去做:

 let data = CellDataDisplayInformation()
 cell.barName?.text = data.namesArray[indexPath.row]

每個單元格的所有標簽均保持不變,並且不會隨機播放,但是當我在每個新單元格上滾動時,視圖會滯后。

因此,總之,在表單元格中顯示數據時如何避免亂碼和滯后?

我的DailyBarCellViewTableViewCell類:

  class DailyBarCellViewTableViewCell: UITableViewCell {

// MARK: Properties

    struct Static  {
    static var data = CellDataDisplayInformation()
    }

@IBOutlet weak var barName: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
    self.barName.text = nil
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

override func prepareForReuse() {
    self.barName.text = nil
    super.prepareForReuse()
  }

}

在您返回-cellForRowAtIndexPathcell后, dispatch_async調用會導致cell.barName?.text = data.namesArray[indexPath.row]被執行。 您不應該這樣做,因為在獲取數據時,這些單元格可能已經被重新用於其他行。

無需在該特定cell上設置文本,而是需要讓表為要為其獲取數據的索引出售一個單元格。 如果在獲取數據時該索引仍在屏幕上,則tableView將為您提供正確的單元格。 否則,返回nil。

像這樣:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! DailyBarCellViewTableViewCell

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0)) {

        let data = CellDataDisplayInformation()

        dispatch_async(dispatch_get_main_queue()) {

            //do it this way
            if let futureCell = tableView.cellForRowAtIndexPath(indexPath) as? DailyBarCellViewTableViewCell {
                futureCell.barName?.text = data.namesArray[indexPath.row]
            }
        }
    }

    return cell
}

暫無
暫無

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

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