簡體   English   中英

使用插入功能時奇怪的tableView動畫

[英]weird tableView animation when using insert function

當用戶到達表格底部時,我試圖在表格視圖中插入更多單元格。 插入工作正常,但向上滾動時發生奇怪的動畫。

我試圖在insert()方法之前和之后添加beginUpdates()endUpdates() 但這沒有幫助。 我試圖在viewDidLoad添加tableView.contentInsetAdjustmentBehavior = .never ,但這沒有幫助。

我嘗試的代碼是:

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) {
            loadMoreCells()
        }

func loadMoreCells() {
       ServerRequests.getDataServerRequest(id: provider.ID, page: page) { (foundEpisodes) in
            if foundEpisodes.count > 0 {
                let previous = self.episodes.count
                self.episodes.insert(contentsOf: foundEpisodes, at: self.episodes.count)
                self.isViewOpen.insert(contentsOf: [Bool](repeatElement(false, count: foundEpisodes.count)), at: self.isViewOpen.count)
                var indexPathsToBeInserted = [IndexPath]()
                for i in previous..<self.episodes.count{
                    indexPathsToBeInserted.append(IndexPath(row: i, section: 0))
                }
                self.tableView.insertRows(at: indexPathsToBeInserted, with: UITableViewRowAnimation.none)
            }
        }
    }

我在錯誤的地方打電話嗎? 還是我做錯了什么?

UPDATE

viewDidLoad

tableView.register(UINib(nibName: NIB_NAME, bundle: nil), forCellReuseIdentifier: NIB_IDENTIFIRE)

cellForRow

let cell = tableView.dequeueReusableCell(withIdentifier: NIB_IDENTIFIRE, for: indexPath) as! EpisodePerProviderTableViewCell
    cell.setUpCell(tableView: tableView, episode: episodes[indexPath.row], indexPath: indexPath, isViewOpen: isViewOpen)

    return cell

setUpCell函數為:

unc setUpCell(tableView: UITableView, episode: Episode, indexPath: IndexPath, isViewOpen: [Bool]) {
    isMenuVisible = false
    menuView.isHidden = true
    if (isViewOpen[indexPath.row]){
        descriptionLabel.numberOfLines = 100
        moreLessLabel.text = "Less"
    }else{
        descriptionLabel.numberOfLines = 2
        moreLessLabel.text = "More"
    }
    tableView.beginUpdates()
    tableView.endUpdates()

    self.isViewOpen = isViewOpen
    self.indexPath = indexPath
    self.tableView = tableView
    self.episode = episode
    arrowButton.transform = .identity

    //set up cell content
    if let myDate = dateFormatter.date(from: episode.episodePostDate) {
        postedDate.text = dateFormatter.timeSince(from: myDate as NSDate)
    }
    episodeImage.windless.start()
    episodeImage.sd_setImage(with: URL(string: episode.episodeImageUrl ?? ""), placeholderImage: UIImage(named: "FalloundPlayerLogo")) { (providerImage, error, SDImageCacheType, url) in
        self.episodeImage.windless.end()
    }
    title.text = episode.episodeName
    durationLabel.text = "".formatted(time: Float(episode.episodeDuration), withoutSec: true)
    descriptionLabel.text = episode.episodeDescription
}

我認為問題是因為loadMoreCells被調用了太多次。

嘗試使用此檢查代替willDisplay:

if indexPath.row == yourArrayOfCells.count - 1{
    loadMoreCells()
}

UPDATE

所以我有一些相同的問題,經過一番挖掘之后,我自己找到了這個解決方案:

//conform to UIScrollViewDelegate

let threshold : CGFloat = 10.0 // threshold from bottom of tableView
var isLoadingMore = false //checks if API session ended

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let contentOffset = scrollView.contentOffset.y
    let maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;

    if !isLoadingMore && (maximumOffset - contentOffset) <= threshold {

        self.isLoadingMore = true
        DispatchQueue.main.async {
            loadMoreCells() 
            self.isLoadingMore = false
        }
    }
}

如果API請求發生得太快並且負載看起來很有趣,則可以添加一個延遲:

        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            loadMoreCells() 
            self.isLoadingMore = false
        }

如果無法正常工作,我將假定問題不在您提供的代碼中。

UPDATE-2

setUpCell()中的那兩個函數不是必需的:

tableView.beginUpdates()
tableView.endUpdates()

希望這可以幫助。

UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()

self.tableView.insertRows(at: indexPathsToBeInserted, with: UITableViewRowAnimation.none)

self.tableView.endUpdates()
UIView.setAnimationsEnabled(true)

暫無
暫無

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

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