2017-09-07 74 views
-2

想要在tableview底部添加拉動刷新。如何在iOS中快速刷新tableview按鈕swift

首先要在tableview中100的數據顯示,以90

然後添加在拉的實現代碼如下底部刷新。拉它想要顯示80到70,然後拉刷新70到60,然後拉刷新50到40 ...等最終1到10意味着獲取停止顯示「無數據可用」。如何實現這一點。幫助我感謝提前。

在這裏我的代碼。

@IBOutlet weak var dataTbl: UITableView! 
    var numbers: [Any] = ["90","91","92","93","94","95","96","97","98","99","100"] // display first 100 to 90. 

在tableview中加載。

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return self.numbers.count 
    } 



func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

     let cell:UITableViewCell = self.dataTbl.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as UITableViewCell! 
     cell.textLabel?.text = self.numbers[indexPath.row] as? String 

     return cell 
    } 

在谷歌搜索添加在tableview的按鈕刷新。我有這個代碼。使用

spinner.startAnimating() 

隱藏使用

spinner.stopAnimating() 

通過使用此代碼如何

let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
spinner.color = UIColor.darkGray 
spinner.hidesWhenStopped = true 
tableView.tableFooterView = spinner 

顯示微調做拉刷新幫助我。

+0

卻無法實現它。 - 意思是什麼...... –

+0

你想用上面的代碼做什麼? – shahnilay86

+0

你在這裏調用了這個'let spinner = UIActivityIndi​​catorView(activityIndi​​catorStyle:.gray) spinner.color = UIColor.darkGray' –

回答

0

您將需要使用下面的代碼,

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
    if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) { 
      //you reached the bottom of tableview, you can append the other 10 numbers to array and do reload 
    } 
} 

你並不需要添加UIScrollViewDelegate實現這一目標,我希望UITableViewDelegate就足夠了。 As UITableview繼承UIScrollView 的屬性希望這有助於

2

使用下面的委託來檢測表的末尾,併爲表格尾添加微調器邏輯。

func scrollViewDidScroll(_ scrollView: UIScrollView) { 
      if tableView.contentOffset.y >= (tableView.contentSize.height - tableView.frame.size.height) { 
        let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
    spinner.color = UIColor.darkGray 
    spinner.hidesWhenStopped = true 
    tableView.tableFooterView = spinner 
      } 
     } 

一旦你的更多的條目添加到您的數據源,然後刪除頁腳視圖

tableView.tableFooterView = nil 
+0

如何刷新元素100至90,90至80 ,80到70 – saravanar

+0

如果您從服務器獲取它,則需要向服務器發送偏移量以獲取後面的10條記錄,當您從服務器獲得響應後,將這些記錄附加到以前的數據源 – KavyaKavita