eggswift / pull-to-refresh

#Busy Re-Building....# An easy way to use pull to refresh and infinite scrolling in Swift. Pod 'ESPullToRefresh'
MIT License
1.82k stars 252 forks source link

一个关于tableView上拉加载的问题 #22

Closed zkhCreator closed 8 years ago

zkhCreator commented 8 years ago

在上拉加载过程中,我不知道为什么,每次第一次结束之后insert Data之后,他都会回滚到TableView顶部。不知道是我的问题还是框架的问题,而当使用reload Data之后,是不会回滚到顶部的。 代码如下:

VC中的代码:
convenience init(url:String){
        self.init(nibName: nil, bundle: nil)
        self.listModel = ZNListModel.init(baseURL: "http://news.zafu.edu.cn", url:url)

        self.tableView.es_addPullToRefresh {
            [weak self] in
            self?.listModel?.reloadData({
                self?.tableView.es_stopPullToRefresh(completion: true)
            })
        }

        self.tableView.es_addInfiniteScrolling { 
            [weak self] in

            self?.listModel?.addNewInfo({
                self?.tableView.es_stopLoadingMore()
            })

        }
    }

//数据解析完后的insert
//MARK:Delegate
    func finishLoadListView() {
//        if self.tableView.numberOfRowsInSection(0) < listModel?.listArray.count {
//            let startCount = self.tableView.numberOfRowsInSection(0)
//            let endCount = listModel?.listArray.count
//            
//            var indexPathArr:Array<NSIndexPath> = []
//            
//            for row in startCount ..< endCount! {
//                indexPathArr.append(NSIndexPath.init(forRow: row, inSection: 0))
//            }
//            
//            self.tableView.insertRowsAtIndexPaths(indexPathArr, withRowAnimation: UITableViewRowAnimation.Top)
////            self.tableView.reloadRowsAtIndexPaths(indexPathArr, withRowAnimation: UITableViewRowAnimation.Bottom)
//        }else{
            self.tableView.reloadData()
//        }
    }

//model中的代码
func addNewInfo(callback:(()->())?) {
        currentPage += 1;

        let q = dispatch_queue_create("async_queue", DISPATCH_QUEUE_SERIAL)
        let url:String = self.url!+"?page=\(currentPage)"

        dispatch_sync(q) { 
            self.getMessage(url, callback: callback)
        }
    }

func getMessage(url:String,callback:(()->())?) {
        let JiCode = Ji.init(htmlURL: NSURL(string: url)!)

        let content = JiCode?.xPath("//div[starts-with(@class,'content-right')]/ul/li/a")
        let dates = JiCode?.xPath("//div[starts-with(@class,'content-right')]/ul/li/span")

        if content != nil {
            for item in content! {
               //...解析内容(因为过多已经省去)

                //            将对应的顺序放入Array中
                self.listArray.append((title,urlStr,date,num))
            }

            let mainQueue = dispatch_get_main_queue()

            dispatch_async(mainQueue, {
                if callback != nil{
                    callback!()
                }
                self.delegate?.finishLoadListView()
            })
        }
    }
eggswift commented 8 years ago

Hi 可以分享一个demo吗? 这样我可以很容易定位问题,并且解决它~