【发布时间】:2019-04-14 10:20:03
【问题描述】:
当用户到达表格底部时,我正在尝试在表格视图中插入更多单元格。插入工作正常,但是向上滚动时会发生奇怪的动画。
我尝试在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)
}
}
}
我是否在错误的地方调用了插入?或者我做错了什么?
更新
在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() 吗?
-
@VadimF。当用户到达表格底部时,或者换句话说,当最后一个单元格被加载时。
标签: ios swift uitableview animation uiscrollview