2017-02-25 24 views
2

我想在顯示最後一個單元格的同時向我的UITableView的底部添加一個ActivityIndi​​cator,同時我獲取更多數據,然後在獲取數據時隱藏它。在加載時在UITableView的底部添加一個活動指示器

因此,我滾動到底部 - >顯示的最後一行 - >微調開始旋轉,而數據被提取 - >數據提取,隱藏微調 - >新的數據添加到tableview。

有關如何實現此目的的任何提示?

感謝;)

回答

16

添加此功能

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { 
    let lastSectionIndex = tableView.numberOfSections - 1 
    let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1 
    if indexPath.section == lastSectionIndex && indexPath.row == lastRowIndex { 
     // print("this is the last cell") 
     let spinner = UIActivityIndicatorView(activityIndicatorStyle: .gray) 
     spinner.startAnimating() 
     spinner.frame = CGRect(x: CGFloat(0), y: CGFloat(0), width: tableView.bounds.width, height: CGFloat(44)) 

     self.tableview.tableFooterView = spinner 
     self.tableview.tableFooterView?.isHidden = false 
    } 
} 

和tableFooterView應該隱藏在數據負載。

+1

由於屏幕尺寸不同,請勿使用修正尺寸值 – lukwuerz

+0

什麼是tableView_category? – lukwuerz

+0

tableview_category是包含行的默認tableView。我得到了這個和這個例子工作像一個魅力 –

2

一種方法是添加自定義單元格,在它UIActivityIndi​​catorView。你可以把它放在一個單獨的部分。有很多方法可以做到這一點。

或者你看看這個:https://github.com/evnaz/ENFooterActivityIndicatorView

+0

我來看看 –

+1

我不想僅僅爲此而使用第三方庫,並且給出的示例用Obj-C編寫 –

+1

您不必使用它。你可以檢查它,知道它是如何工作的,如果需要的話可以快速寫入。 – lukwuerz

相關問題