iOS UITableView: what's the different between “cellForRowAtIndexPath” and “willDisplayCell: forRowAtIndexPath:”

前端 未结 6 974
别那么骄傲
别那么骄傲 2020-11-30 01:06

Just as the question\'s title mentions: What\'s the difference between \"cellForRowAtIndexPath\" and \"willDisplayCell: forRowAtIndexPath

6条回答
  •  春和景丽
    2020-11-30 01:39

    Despite what might seem intuitive, willDisplay cell: is called immediately after cellForRowAt indexPath: is called.

    I had an app where images and video would be loaded in from either a URLCache or downloaded and displayed in the cell. I noticed whenever I'd start my application all the videos and images would load before I could see them and I noticed this because I could hear the audio from the last video in the tableView while looking at the first item in the tableView. This is with 8 items in the tableView. I printed to the console to get a better idea of what the order of the delegate functions calls were.

    The results:

    • Created cell at row 0
      • Drew feed cell at row 0
    • Created cell at row 1
      • Drew feed cell at row 1
    • Created cell at row 2
      • Drew feed cell at row 2
    • Created cell at row 3
      • Drew feed cell at row 3
    • Created cell at row 4
      • Drew feed cell at row 4
    • Created cell at row 5
      • Drew feed cell at row 5
    • Created cell at row 6
      • Drew feed cell at row 6
    • Created cell at row 7
      • Drew feed cell at row 7
    • Stopped showing cell at row 2
    • Stopped showing cell at row 3
    • Stopped showing cell at row 4
    • Stopped showing cell at row 5
    • Stopped showing cell at row 6
    • Stopped showing cell at row 7

    The Code:

        override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "feedCell", for: indexPath) as! FeedCell
        print("Created cell at row \(indexPath.row)")
        let post = posts[indexPath.row]
        cell.post = post
        cell.viewController = self
        cell.configureCell()
        return cell
    }
    
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let feedCell = cell as? FeedCell{
            print("Drew feed cell at row \(indexPath.row)")
            // only want download task to start when the cell will display
            if feedCell.post.isVideo {
                feedCell.loadVideo()
            } else {
                feedCell.loadImage()
            }
        }
    }
    
    override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        print("Stopped showing cell at row \(indexPath.row)")
        if let feedCell = cell as? FeedCell{
            feedCell.player?.pause()
        }
    }
    

提交回复
热议问题