UITableView load more when scrolling to bottom like Facebook application

后端 未结 18 2153

I am developing an application that uses SQLite. I want to show a list of users (UITableView) using a paginating mechanism. Could any one please tell me how to load more dat

18条回答
  •  再見小時候
    2020-11-27 09:48

    The best way to solve this problem is to add cell at the bottom of your table, and this cell will hold indicator.

    In swift you need to add this:

    1. Create new cell of type cellLoading this will hold the indicator. Look at the code below
    2. Look at the num of rows and add 1 to it (This is for loading cell).
    3. you need to check in the rawAtIndex if idexPath.row == yourArray.count then return Loading cell.

    look at code below:

    import UIKit
    
    class LoadingCell: UITableViewCell {
    
    @IBOutlet weak var indicator: UIActivityIndicatorView!
    
    
    }
    

    For table view : numOfRows:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  yourArray.count + 1
    }
    

    cellForRawAt indexPath:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        if indexPath.row == users.count  {
            // need to change
            let loading = Bundle.main.loadNibNamed("LoadingCell", owner: LoadingCell.self , options: nil)?.first as! LoadingCell
            return loading
    
        }
    
        let yourCell = tableView.dequeueReusableCell(withIdentifier: "cellCustomizing", for: indexPath) as! UITableViewCell
    
        return yourCell
    
    }
    

    If you notice that my loading cell is created from a nib file. This videos will explain what I did.

提交回复
热议问题