Taking my recent microproject as an example, let's talk about how to pull and load data from pull-down refresh.
Page loading data:
# Pragma mark-load Weibo data-(void) loadstatusdata {_ statusesframe = [nsmutablearray array]; // Add [statusmanage getstatuseswithsendsinceid: 0 maxid: 0 success: ^ (nsarray * statues) {for (status * s in statues) {statuscellframe * f = [[statuscellframe alloc] init]; F. status = s; [_ statusesframe addobject: F];} [self. tableview reloaddata];} failure: ^ (nserror * error) {nslog (@ "% @", error) ;}];}
Pull up Refresh:
# Pragma mark-load the latest data-(void) loadnewdata :( mjrefreshbaseview *) refreshview {// 1. retrieve the ID statuscellframe * SCF = [_ statusesframe firstobject]; long since = [[SCF status] id]; // 2. get Weibo data after sence [statusmanage getstatuseswithsendsinceid: Since maxid: 0 success: ^ (nsarray * statues) {// 1 ". get the latest Weibo data and add it to the corresponding frame nsmutablearray * newstatusframearray = [[nsmutablearray alloc] init]; for (status * s in statues) {statuscellframe * f = [[statuscellframe alloc] init]; F. status = s; [newstatusframearray addobject: F];} // 2. insert the array to the front of the first microblog [_ statusesframe insertobjects: newstatusframearray atindexes: [nsindexset indexsetwithindexesinrange: nsmakerange (0, newstatusframearray. count)]; // 3. refresh the table [self. tableview reloaddata]; // 4. let the refresh control stop refreshing status [refreshview endrefreshing]; [self shownewstatuscount: newstatusframearray. count];} failure: ^ (nserror * error) {[refreshview endrefreshing];}
Pull down to load more:
# Pragma mark-load more data-(void) loadmoredata :( mjrefreshbaseview *) refreshview {// 1. last microblog ID statuscellframe * f = [_ statusesframe lastobject]; long last = [F. status id]; // 2. get Weibo data [statusmanage getstatuseswithsendsinceid: 0 maxid: Last-1 success: ^ (nsarray * statues) {// 1. calculate its frame nsmutablearray * oldstatusframearray = [nsmutablearray array] while obtaining the latest Weibo data; for (status * s in statues) {statuscellframe * f = [[statuscellframe alloc] init]; F. status = s; [oldstatusframearray addobject: F];} // 2. insert newframes to the end of the old data [_ statusesframe addobjectsfromarray: oldstatusframearray]; // 3. refresh the table [self. tableview reloaddata]; // 4. let the refresh control stop refreshing status [refreshview endrefreshing]; [self shownewstatuscount: oldstatusframearray. count];} failure: ^ (nserror * error) {[refreshview endrefreshing];}
TIPS:
Load more from the drop-down list. The obtained array can be directly added to the end of the last object of the array on the current display page,
// 2. Insert newframes to the end of the old data [_ statusesframe addobjectsfromarray: oldstatusframearray];
Pull up and refresh, the entire array must be inserted before the first object of the current display page array, because the data returned by Sina is larger according to the shorter time.
// 2. Insert the array into the front of the first microblog [_ statusesframe insertobjects: newstatusframearray atindexes: [nsindexset indexsetwithindexesinrange: nsmakerange (0, newstatusframearray. Count)];