AZTableView 0.0.3

AZTableView 0.0.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Jan 2018
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Afroz Zaheer.



  • By
  • AfrozZ

AZTableView Controller

Alt text

Features

  • Automatic pagination handling
  • No more awkward empty TableView
  • AZ TableView controller give you advantage to connect your (Loading, no result, error ) views via Interface builder

New in version 0.0.2

  • You can now add your custome xib as dummy views (loading, error, no result)
  • You can xib based next page loading cell

Usage

Step 1

  • Extend you view controller from AZTableVIewController
class ViewController: AZTableViewController {

    var lastIndex = 0
    var results = [String]()
    override func viewDidLoad() {

/*      self.loadLoadingView(nibName: "") // if bundle is nil
        self.loadLoadingView(nibName: "", bundle: myBunlde) // if custom bundle */ 
// if want to load views from .XIB files 

        super.viewDidLoad()
        self.fetchData()
    }

    override func viewDidAppear(_ animated: Bool) {

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

Step 2

  • Set the next page loder cell outlet as given below,

Alt text

Alt text

Step 3

  • Confirm you controller to UITableViewDelegate and UITableViewDataSource

  • And override AZtabeView cellForRow function.

extension ViewController : UITableViewDataSource, UITableViewDelegate {

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

        if let cell = tableView.dequeueReusableCell(withIdentifier: "Cell")
        {
            cell.textLabel?.text = results[indexPath.row]
            return cell
        }
        return UITableViewCell()
    }
    
    override func AZtableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return results.count
    }
}

Step 4

  • Override two more functions of fetchData and fetchNextData functions
//MARK: - API Call
extension ViewController {
    
    override func fetchData() {
        super.fetchData()
        
        FakeService.getData { (error, results) in
           
            if let resu = results {
                self.results.removeAll()
                self.results.append(contentsOf: resu)
                self.didfetchData(resultCount: resu.count, haveMoreData: true)
            }
                
            else if let error = error {
                self.errorDidOccured(error: error)
            }
        }
    }
    
    override func fetchNextData() {
        super.fetchNextData()
        
        FakeService.getData (offset: results.count) { (error, results) in
            
            if let resu = results {
                
                self.results.append(contentsOf: resu)
                if self.results.count < 45 { // you probably get next page exist from service.
                    self.didfetchData(resultCount: resu.count, haveMoreData: true)
                }
                else {
                    self.didfetchData(resultCount: resu.count, haveMoreData: false)
                }
            }
            else if let error = error {
                self.errorDidOccured(error: error)
            }
        }
    }
}

License

AZTableView is available under the MIT license. See the LICENSE file for more info.

Author

Afroz Zaheer - (https://github.com/AfrozZaheer)