当用户到达表格底部时,我试图在表格视图中插入更多的单元格。插入工作正常,但向上滚动时发生奇怪的动画。
我尝试在beginUpdates()
方法之前和之后添加endUpdates()
和insert()
。但这没有帮助。我试图在tableView.contentInsetAdjustmentBehavior = .never
中添加viewDidLoad
,但没有帮助。
我尝试的代码是:
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)
}
}
}
我在错误的地方打电话给插入物吗?还是我做错了什么事?
更新
在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
}
答案 0 :(得分:2)
我认为问题是因为loadMoreCells被调用了太多次。
尝试使用此检查代替willDisplay:
SELECT price
FROM table
ORDER BY price DESC
LIMIT 50;
更新
因此,在进行一些挖掘之后,我也遇到了同样的问题:
if indexPath.row == yourArrayOfCells.count - 1{
loadMoreCells()
}
如果API请求发生得太快并且负载看起来很有趣,则可以添加一个延迟:
//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
}
}
}
如果无法正常工作,我将认为问题不在您提供的代码中。
UPDATE-2
setUpCell()中的那2个功能不是必需的:
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
loadMoreCells()
self.isLoadingMore = false
}
答案 1 :(得分:0)
希望这会有所帮助。
UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.insertRows(at: indexPathsToBeInserted, with: UITableViewRowAnimation.none)
self.tableView.endUpdates()
UIView.setAnimationsEnabled(true)