10 minutes to build the mainstream framework _ pull-up/pull-up Refresh data (OC)

Source: Internet
Author: User

Topics in this section (Network section-dropdown/pull-up refresh)
    • Source address at the end of the article
    • Achieve results

      Drop-down Refresh data

      Pull up to load more data
Objective
    • After 10 minutes to build the mainstream framework _ Simple Network part (OC) Introduction, I believe you have achieved the basic Internet access to data, but only rough access to fixed data, let us implement the drop-down refresh and pull load more. Again, let's do the preparatory work first.
Preparatory workGitHub looks for excellent third-party refresh frameworks
    • 1. The ancestors planted trees, posterity. Excellent third-party framework can be used, of course, is very good, can greatly improve our development efficiency, such as special needs can only be handwritten on their own except

      Refresh
    • 2. Access to the Readme file of the third-party framework (reader's own reading)
    • 3. Integrate into your project using Cocoapods
The basic realization idea of refreshDrop-down Refresh
    • 0. Since this article focuses only on the implementation of the feature, we can understand the refresh as loading the latest data from the server by putting the principle aside.
    • 1. In the Viewdidload initialization drop-down refresh the control and bind the method that pulls the refresh (Request network data), when the drop-down refresh is triggered, the LoadData method is called. Self.tableView.header = [Mjrefreshnormalheader headerwithrefreshingtarget:self refreshingaction: @selector (loaddata )];
    • 2. Implement the request network data within the LoadData (see the 10-minute build Mainstream framework _ Simple Network section (OC)).
    • 3. Successful request, stop refresh control [WeakSelf.tableView.header endrefreshing];
    • 4. Request failed, stop refresh control [WeakSelf.tableView.header endrefreshing];
Pull up load more
    • 0. Since our implementation is the MVC pattern, it can be easily understood as: Modify the data of the model, the View interface display will also change.
    • 1. Define the ' page Number ' property (for loading the parameters to be passed on the next page) @property (nonatomic,assign) Nsinteger PN;
    • 2. Initialize the pull-up refresh control and bind pull-up to load more methods, and when the pull-up load event is triggered, the Loadmoredata method is called. Self.tableView.footer = [Mjrefreshautonormalfooter footerwithrefreshingtarget:self refreshingaction: @selector ( Loadmoredata)];
    • 3. Basic logic for loading more data within Loadmoredata
      • (1) The request parameter is changed, plus params[@ "pn" = @ (SELF.PN);
      • (2) Add ' page number ' WEAKSELF.PN + + in the block where the request is successful;
      • (3) Use the new model array to receive the data in the block where the request was successful nsarray *array = [Cyxmenu objectarraywithkeyvaluesarray:responseobject[@ "result"]; and stitching to the tail of the original array [Weakself.menus Addobjectsfromarray:array];
      • (4) Refresh form [Weakself.tableview Reloaddata];
      • (5) Stop refresh control [WeakSelf.tableView.footer endrefreshing];
Detailed implementation steps on the code(a) drop down
  • Initializing the head refresh control within setuptable
    //Head Refresh control  Self . TableView . Header = [Mjrefreshnormalheader headerwithrefreshingtarget: self refreshingaction:@selector (LoadData)];  [self. TableView. Header Beginrefreshing];
  • Implementing the LoadData Method

    /** * Send request and get Data method */- (void) loaddata{[ Self. Manager. TasksMakeobjectsperformselector:@selector(cancel)]; Self. PN=1;//Request parameters (written according to the interface documentation) nsmutabledictionary*params = [nsmutabledictionaryDictionary]; params[@ "Menu"] =@ "Tomato"; params[@ "PN"] = @( Self. PN); params[@ "RN"] =@ "Ten"; params[@ "Key"] =@ "2ba215a3f83b4b898d0f6fdca4e16c7c";//used in block AFN to prevent circular references__Weak typeof( Self) Weakself = Self; [ Self. Manager. Responseserializersetacceptablecontenttypes:[NssetSetwithobject:@ "text/html"]]; [ Self. ManagerGet:cyxrequesturl Parameters:params success:^ (Nsurlsessiondatatask* _nonnull task,ID_nonnull responseobject) {NSLog(@ "Request succeeded");//using the Mjextension Framework for dictionary-to-modelWeakself. Menus= [Cyxmenu objectarraywithkeyvaluesarray:responseobject[@ "Result"]]; Weakself. PN++;//Refresh data (cannot be displayed if data is not refreshed)[Weakself. TableViewReloaddata]; [Weakself. TableView. HeaderEndrefreshing]; } failure:^ (Nsurlsessiondatatask* _nonnull task,Nserror* _nonnull error) {NSLog(@ "Request failed reason:%@", error); [Weakself. TableView. HeaderEndrefreshing];  }]; }
(b) Pull-up
    • Initializing a pull-up refresh control within setuptable

      //tail flush Control  Self . TableView . Footer = [mjrefreshautonormalfooter footerwithrefreshingtarget: self refreshingaction:@ Selector(loadmoredata)];
    • Implementing the Loadmoredata Method

/** * Load More Data * /- (void) loadmoredata{[ Self. Manager. TasksMakeobjectsperformselector:@selector(cancel)];//Request parameters (written according to the interface documentation) nsmutabledictionary*params = [nsmutabledictionaryDictionary]; params[@ "Menu"] =@ "Tomato"; params[@ "PN"] = @( Self. PN); params[@ "RN"] =@ "Ten"; params[@ "Key"] =@ "2ba215a3f83b4b898d0f6fdca4e16c7c";//used in block AFN to prevent circular references__Weak typeof( Self) Weakself = Self; [ Self. Manager. Responseserializersetacceptablecontenttypes:[NssetSetwithobject:@ "text/html"]]; [ Self. ManagerGet:cyxrequesturl Parameters:params success:^ (Nsurlsessiondatatask* _nonnull task,ID_nonnull responseobject) {//using the Mjextension Framework for dictionary-to-model Nsarray*array = [Cyxmenu objectarraywithkeyvaluesarray:responseobject[@ "Result"]]; [Weakself. MenusAddobjectsfromarray:array]; Weakself. PN++;//Refresh data (cannot be displayed if data is not refreshed)[Weakself. TableViewReloaddata]; [Weakself. TableView. FooterEndrefreshing]; } failure:^ (Nsurlsessiondatatask* _nonnull task,Nserror* _nonnull error) {NSLog(@ "Request failed reason:%@", error); [Weakself. TableView. FooterEndrefreshing];    }]; }
    • It has been easy to implement the pull-up and pull-up refresh function, the next time to discuss more other features it ^_^
    • Attached: source GitHub address

10 minutes to build the mainstream framework _ pull-up/pull-up Refresh data (OC)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.