首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UICollectionView在重新加载数据时不平滑地滚动

UICollectionView在重新加载数据时不平滑地滚动
EN

Stack Overflow用户
提问于 2016-12-09 10:30:43
回答 6查看 6.6K关注 0票数 2

在我的UICollecitonView中,我使用

self.DataTableCollectionView.reloadData()

但是,随着我的集合中的数据越来越多,它的滚动变得粗糙,在加载了一些页面之后,就很难向上或向下滚动。它被卡住了。

最后,当我尝试一次加载1000行时,滚动工作平稳,但是当我尝试使用API一次分页并加载50行时,就很难滚动。

有谁能帮我解决这个问题?

我搞不清是怎么回事?self.DataTableCollectionView.reloadData()可对此作出响应吗?

下面是加载滚动数据的代码:

代码语言:javascript
运行
复制
func scrollViewDidScroll(scrollView: UIScrollView) {

     let offsetY = scrollView.contentOffset.y
     let contentHeight = scrollView.contentSize.height

     if(offsetY > (contentHeight - scrollView.frame.size.height)){              

         let seconds = 3.0
         let delay = seconds * Double(NSEC_PER_SEC)
         let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))

         self.loadMoreData()
         dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
             self.DataTableCollectionView.reloadData()

         })                
      }        

}

==========

我在重复使用这些细胞

代码语言:javascript
运行
复制
self.DataTableCollectionView .registerNib(UINib(nibName: "DCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: dCellIdentifier)
self.DataTableCollectionView .registerNib(UINib(nibName: "CCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: cCellIdentifier) 

以及重复使用:

代码语言:javascript
运行
复制
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let dCell : DCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(dCellIdentifier, forIndexPath: indexPath) as! DCollectionViewCell

let cCell : ContentCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(cCellIdentifier, forIndexPath: indexPath) as! CCollectionViewCell       
}

=========

添加更具体的代码:

现在我更新了我的滚动功能

代码语言:javascript
运行
复制
func scrollViewDidScroll(scrollView: UIScrollView) {
    let offsetY = scrollView.contentOffset.y
    let contentHeight = scrollView.contentSize.height
    if(offsetY > (contentHeight - scrollView.frame.size.height)){
       var returnTableData = NSMutableArray()

       self.InspectPageId = self.InspectPageId+1
       self.dataSource.populateData(PageId:self.InspectPageId)
       returnTableData = self.dataSource.DTableDataArray

       if(returnTableData.count>0){
          for i in 0..<returnTableData.count {
             self.TableDataArray.addObject(returnTableData[i])
          }
       }
       self.NumberOfRows = self.TableDataArray.count

       dispatch_async(dispatch_get_main_queue(),{
          self.DataTableCollectionView.reloadData()
       })
   }
}

以下是手动创建集合视图的代码:

代码语言:javascript
运行
复制
if indexPath.row == 0 {
//Here Header Cell is creating
                let dateCell : DateCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(dateCellIdentifier, forIndexPath: indexPath) as! DateCollectionViewCell
                dateCell.dateLabel.font = UIFont(name:"HelveticaNeue", size: 12)
                dateCell.dateLabel.textColor = UIColor.blackColor()
                dateCell.dateLabel.textAlignment = .Left
                dateCell.dateLabel.text = UMIDDataArray[rowIndex-1] as? String
                dateCell.backgroundColor = UIColor.whiteColor()
                dateCell.layer.shouldRasterize = true
                dateCell.layer.rasterizationScale = UIScreen.mainScreen().scale
                return dateCell
            } else {
//Here Data Cell is Creating
                let contentCell : ContentCollectionViewCell = collectionView .dequeueReusableCellWithReuseIdentifier(contentCellIdentifier, forIndexPath: indexPath) as! ContentCollectionViewCell
                contentCell.contentLabel.font = UIFont(name:"HelveticaNeue", size: 12)
                contentCell.contentLabel.textColor = UIColor.blackColor()
                contentCell.contentLabel.textAlignment = .Left
                contentCell.bottomBorder.hidden  = false
                contentCell.layer.shouldRasterize = true
                contentCell.layer.rasterizationScale = UIScreen.mainScreen().scale

                let LensData:NSArray =  TableDataArray[rowIndex-1] as! NSArray
                let ColumnValue = LensData[colIndex-1] as? String
                contentCell.contentLabel.text = ColumnValue
                contentCell.backgroundColor = UIColor.whiteColor()

                return contentCell
            }

Following is the code which is use to take data from API:

//function of DataSource Class.
func populateData(PageId:Int){
var returnData = NSMutableArray()
let OtherColobjects = NSMutableArray()

let Urlpath = SetApiUrl()
let url: NSURL = NSURL(string: Urlpath)!
let urlRequest = NSURLRequest(URL: url)

let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

let semaphore = dispatch_semaphore_create(0)
let task = session.dataTaskWithRequest(urlRequest) {
(data, response, error) in

guard error == nil else {
return
}

guard let responseData = data else {
return
}

do {
guard let Data = try NSJSONSerialization.JSONObjectWithData(responseData, options: []) as? [String: AnyObject] else {
return
}

for anItem in Data["result"]!["DATA"] as! [Dictionary<String, AnyObject>] {
let d1 = self.checkForNull(anItem["data1"]!)
let d2 = self.checkForNull(anItem["data2"]!)
let d3 = self.checkForNull(anItem["data3"]!)
let d4 = self.checkForNull(anItem["data4"]!)
...... // Total 20 Columns

let obj = [ d1, d2, d3, d4,.............,d20 ]

OtherColobjects.addObject(obj)
}
dispatch_semaphore_signal(semaphore)
} catch {
return
}
}
task.resume()

dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
self.NumberOfRows = returnData[0].count
self.DTableDataArray = returnData[1] as! NSMutableArray
}
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2016-12-13 04:42:34

您应该只从主线程更新您的UI,所以请这样做.

Swift 2.x

代码语言:javascript
运行
复制
           dispatch_async(dispatch_get_main_queue()), {
                self.DataTableCollectionView.reloadData()
            }

Swift 3

代码语言:javascript
运行
复制
DispatchQueue.main.async {
    self.DataTableCollectionView.reloadData()
}

如果这不能解决问题,那么您可能还需要在后台线程上调用loadMoreData() .

Swift 2.x

代码语言:javascript
运行
复制
        dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0)){
             self.loadMoreData()
        }

Swift 3

代码语言:javascript
运行
复制
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
     self.loadMoreData()
}

还不清楚您的代码是如何工作的,因为您还没有向我们展示您的loadMoreData()函数。实际上,这似乎意味着从DataTableCollectionView.reloadData()内部调用loadMoreData,但我无法在没有看到函数的情况下判断。请添加附加代码

票数 1
EN

Stack Overflow用户

发布于 2016-12-09 11:32:13

在排队列单元格后添加以下2行,

代码语言:javascript
运行
复制
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
票数 2
EN

Stack Overflow用户

发布于 2016-12-09 10:35:33

您应该将reloadData()发送到MainThread上。根据苹果的文档,操作UI应该总是在MainThread中完成。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41058298

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档