Załaduj więcej po przejściu na dół UITableView

Dla mojej aplikacji używam właściwości" load more at bottom", jak poniżej. W rzeczywistości działa dobrze; jedynym problemem jest to, że gdy użytkownik dociera do przycisku, podczas gdy funkcja load more działa, użytkownikowi wydaje się, że aplikacja jest zamrożona na chwilę, ponieważ nie ma widoku animacji, jak w UIRefreshcontrol. Jak zrobić animację pokazującą, dopóki nowe dane nie zostaną załadowane. Znalazłem kilka właściwości UIBottomrefreshcontrol jako oddzielne biblioteki, ale wszystkie były w Objective-c.

override func viewDidLoad() {
    super.viewDidLoad()

   refreshResults()

 }

 func refreshResults() {


    refreshPage = 40

    // here the query starts
}


func refreshResults2() {

     // here the query starts to add new 40 rows of data to arrays

}


func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,     forRowAtIndexPath indexPath: NSIndexPath) {

    if indexPath.row == refreshPage-1 {
        refreshPage = refreshPage + 40

        refreshResults2()

    }
}
Author: vacawama, 2015-09-06

2 answers

Dodaj UIActivityIndicatorView (tj. spinner) do kontrolera UITableViewController. Podłącz gniazdko do kodu:

@IBOutlet weak var spinner: UIActivityIndicatorView!

Dodaj właściwość do UITableViewController, aby śledzić, że aktualnie ładujesz więcej danych, aby nie próbować zrobić tego dwa razy:

var loadingData = false

Uruchom animację spinnera, a następnie wywołaj refreshResults2():

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    if !loadingData && indexPath.row == refreshPage - 1 {
        spinner.startAnimating()
        loadingData = true
        refreshResults2()
    }
}

Uruchom refreshResults2() na wątku w tle. Pozwoli to na swobodne poruszanie się przy stole. Animowany spinner powie użytkownikowi, że nadchodzi więcej danych. Po zapytaniu zwraca, aktualizuje dane tabeli w głównym wątku.

func refreshResults2() {
    dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0)) {
        // this runs on the background queue
        // here the query starts to add new 40 rows of data to arrays

        dispatch_async(dispatch_get_main_queue()) {
            // this runs on the main queue
            self.refreshPage += 40
            self.tableView.reloadData()
            self.spinner.stopAnimating()
            self.loadingData = false
        }
    }
}
 23
Author: vacawama,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-06 19:42:19

  • Swift 3, Xcode 8
  • func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        let lastElement = dataSource.count - 1
        if !loadingData && indexPath.row == lastElement {
            spinner.startAnimating()
            loadingData = true
            loadMoreData()
        }
    }
    

    Jak na @ vacawama asynchroniczne wywołanie w swift 3:-

    func loadMoreData() {
        DispatchQueue.global(qos: .background).async {
            // this runs on the background queue
            // here the query starts to add new 10 rows of data to arrays
            DispatchQueue.main.async {
                 // this runs on the main queue
                    self.spinner.stopAnimating()
                    self.loadingData = false
    
            }
        }
    }
    
     2
    Author: Abdul Karim,
    Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
    2017-06-12 07:44:35