我目前在使用 UIRefreshControl 刷新collectionview
Feed时遇到问题。刷新工作正常,但是重复了cells
的数量,并继续向 movieDays array
和 sort < / strong>随机重新排列项的顺序。我还尝试将每次刷新时将array
设置为nil
,但是随后出现一个致命错误,指出indexPath
超出范围。反正知道正确的设置方法吗?
非常感谢
override func viewDidLoad() {
super.viewDidLoad()
let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(handleRefresh), for: .valueChanged)
collectionView.refreshControl = refreshControl
self.extendedLayoutIncludesOpaqueBars = true
fetchData()
}
var movieDays: [MoviesByScheduleByDay]? = []
fileprivate func fetchData() {
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
Service.shared.fetchMoviesBySchedule { (movieDays, err) in
if let err = err {
print("Failed to fetch movies", err)
return
}
self.movieDays = movieDays?.data
dispatchGroup.leave()
}
dispatchGroup.enter()
Service.shared.fetchFeaturedMovies { (movies, err) in
if let err = err {
print("Failed to fetch featured movies", err)
return
}
self.headerMovies = movies?.data ?? []
dispatchGroup.leave()
}
dispatchGroup.notify(queue: .main) {
print("complete dispatch")
self.collectionView.reloadData()
self.collectionView.refreshControl?.endRefreshing()
}
}
@objc func handleRefresh() {
fetchData()
}
答案 0 :(得分:0)
尝试一下
"