Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show Placeholder while App is fetching data from API #4

Closed
toseefkhilji opened this issue Sep 27, 2017 · 4 comments
Closed

Show Placeholder while App is fetching data from API #4

toseefkhilji opened this issue Sep 27, 2017 · 4 comments

Comments

@toseefkhilji
Copy link

Hi @malkouz ,

Great Library !!

But,

I want show placeholder while app is getting data from API,
My datasource Array is initialize after parsing data, and meanwhile I need to show placeholder.

So When datasource Array is empty this is not working.

@malkouz
Copy link
Owner

malkouz commented Sep 28, 2017

hello @toseefkhilji ,

Thank you for your contribution, I hope below code will help you:

var list = [Model]()
var isNowLoading = false

func fetchData(){
    isNowLoading = true
    tableView.showLoader()
    tableView.reloadData()
    
    //Start load data from api..
    // ..
    // ..
    // add models to your list
    //the on completion
    self.tableView.hideLoader()
    tableView.reloadData()
    isNowLoading = false
}

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isNowLoading {
        return 5 //as example
    }
    return list.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
    
    if isNowLoading{
        //do nothing
    }else{
        let model = list[indexPath.row]
        //start configure cell content
        //..
    }
    
    return cell
}

@toseefkhilji
Copy link
Author

Hi @malkouz ,

Yes, I'm already doing this.

I thought, If there is some other way of doing this.

Thanks.

@malkouz
Copy link
Owner

malkouz commented Oct 1, 2017

this is the way to make placeholder same of your cell.

you can make custom view placeholder if you want to make customized placeholder.
check example please

@malkouz malkouz closed this as completed Oct 1, 2017
@Ornolfr
Copy link

Ornolfr commented Feb 1, 2018

@malkouz What is the best way to determine the max cell count that can be on screen in order to put it here?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isNowLoading {
        return X // put here
    }
    return list.count
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants