How to implement “Load More Records” in TableView in iPhone SDK?

假如想象 提交于 2019-12-21 02:41:09

问题


I have 10,000 rows in database table. I have a view which is suppose to list all these rows in a tableview. But loading all in one shot takes ages to appear in tableview.

How can we use "Load More Records" feature which will fetch 20 records at a time? If user wants to view more entries, they can click "Load More Records" button and it will show next 20 records.

Will have to modify my select statement? What other changes do I have to do to achieve this?


回答1:


UITableView reuses its cells. This means you can dynamically access your database when cellForRowAtIndexPath: is called- you don't need to load all 10000 elements at once, nor should you. I hope this helps, and that I'm not misunderstanding your question.

This is pretty basic UITableView stuff, but it should get you started. Sorry I don't know much about either Core Data or SQLite.

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell-iPad" owner:self options:nil] lastObject];
        }
        else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
        }
    }


// Do stuff, load database values, etc

    return cell;
}



回答2:


Thanks everyone for your inputs. But I was able to implement this using below link:

http://useyourloaf.com/blog/2010/10/2/dynamically-loading-new-rows-into-a-table.html

Hope this is useful to all.




回答3:


@meetpd its not an answer just a suggestion.

You can use core data instead of sqlite. it will satisfy your requirement of fetching 20 records. It will reduce your code base to 40% (as said by Bard Larson in the Advance Iphone App Development). It is easy to use and faster than sqlite. You can check out the sample code like this :

hope this will help you a great deal of coding.



来源:https://stackoverflow.com/questions/4965919/how-to-implement-load-more-records-in-tableview-in-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!