UIRefreshControl在collectionview中添加重复的单元格

时间:2019-06-16 12:12:42

标签: ios swift

我目前在使用 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()
}

1 个答案:

答案 0 :(得分:0)

尝试一下

"