IOS learning notes 34-egotableviewpullrefresh implement pull-down refresh

Source: Internet
Author: User

One of the scenarios in mobile application development is to refresh the data displayed in the list. You can click the refresh button to refresh the data, and the pull-down refresh function first launched by Twitter is currently the most popular, in iOS, the pull-down method is used to refresh and update the data in uitableview. The most typical is the Sina Weibo client. the pull-down method is used to update the latest Weibo information.

In Android development, there are corresponding open-source projects to implement pull-down refresh. Here we mainly talk about how to implement the pull-down refresh function in IOS. We use an open-source project of egotableviewpullrefresh to implement this function, download the source code first. After the download is complete, there is a demo that can be run directly in the xcode project, and then the source code of this open-source project. You can refer to the demo to learn how to use it, I have implemented some modifications on the basis of the demo, mainly to support the Chinese and English versions. The native only supports English, and I have added support for both Chinese and English versions, then, refresh the time format. The modified format is more intuitive. The native format is the time format that comes with the SDK, and I changed it to a custom format.

First, let's take a look at the project directory structure:


I will not go into details about how to load the source code to the project. Then I created a mainviewcontroller to act as the main interface controller with the corresponding XIB file. The egotableviewpullrefresh folder is the source code of the open-source project. localizable. strings in the supporting files group is an international file that supports both Chinese and English. This file is an international resource file that supports the pull-down and refreshing of Chinese and English display.

Internationalization refers to the switching of the mobile phone language and the text language of the software. I only support Chinese and English here, so only one English and one Chinese file are created. For how to use internationalization in iOS, first create a file in the project, select resouces, and then select a file of the strings file type. After the file is created successfully, select the file, add the language support in the attribute selector on the right, for example:


Click "+" and select the corresponding language. Then, two subfiles are displayed, corresponding to Chinese and English, respectively, in these files, key-value pairs are used to mark the content to be internationalized:

English:

"Loading" = "loading ...";

Chinese:

"Loading" = "loading ...";

The key is on the left and the value on the right. Be sure to end with a semicolon. Otherwise, the key-value pair cannot be recognized.

Usage in code:

Nsstring * loadingstring = nslocalizedstring (@ "loading ",@"");

The first parameter is the key to get the content, and the second parameter is the default value corresponding to the second parameter if the corresponding value of the key cannot be found.
In Android, two strings. xml files are also used for internationalization. Compared with Android, IOS internationalization files are simpler.

Next, let's take a look at how to use the pull-down refresh open-source project to see the final implementation effect:

Open the mainviewcontroller. XIB file, drag it into a uitableviewcontroller, connect datasource and delegate, and declare the uitableview protocol in the mainviewcontroller. h file. Next, run the Code with detailed instructions in the code.

# Import <uikit/uikit. h> # import "egorefreshtableheaderview. H "@ interface mainviewcontroller: uitableviewcontroller <uitableviewdelegate, uitableviewdatasource, Region> {Region * _ refreshtableview; bool _ reloading;} @ property (strong, nonatomic) nsarray * array; // method called when reloading starts-(void) reloadtableviewdatasource; // method called when loading is completed-(void) doneloadingtableviewdata; @ end


# Import "mainviewcontroller. H "@ interface mainviewcontroller () @ end @ implementation mainviewcontroller @ synthesize array = _ array; # pragma mark-# pragma mark view life cycle-(void) viewdidload {[Super viewdidload]; // set the navigation bar title self. navigationitem. title = @ "pull refresh"; if (_ refreshtableview = nil) {// initialize the pull-down refresh control egorefreshtableheaderview * refreshview = [[egorefreshtableheaderview alloc] initwithframe: CG Rectmake (0.0f, 0.0f-self. tableview. bounds. size. height, self. view. frame. size. width, self. tableview. bounds. size. height)]; refreshview. delegate = self; // Add the drop-down refresh control as a sub-control to uitableview [self. tableview addsubview: refreshview]; _ refreshtableview = refreshview;} // initialize nsarray * dataarray = [nsarray arraywithobjects: @ "Ryan", @ "Vivi ", nil]; self. array = dataarray; // reload table data [self. tableview reloadd ATA];}-(void) viewdidunload {[Super viewdidunload]; _ refreshtableview = nil;}-(bool) returns :( uiinterfaceorientation) interfaceorientation {return (interfaceorientation = response );} # pragma mark-# pragma mark uitableviewdatasource methods-(nsinteger) numberofsectionsintableview :( uitableview *) tableview {return 10;}-(nsinteger) tableview :( uitable View *) tableview numberofrowsinsection :( nsinteger) Section {return [self. array count];} // method of title setting for the table with the leading title-(nsstring *) tableview :( uitableview *) tableview titleforheaderinsection :( nsinteger) Section {return [nsstring stringwithformat: @ "title % d", section + 1];}-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellidentifier = @ "celliden Tifier "; uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier: cellidentifier]; If (! Cell) {Cell = [[uitableviewcell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];} cell. textlabel. TEXT = [self. array objectatindex: indexpath. row]; return cell ;}# Pragma mark-# pragma mark data source loading/reloading Methods // method called when reloading starts-(void) reloadtableviewdatasource {_ reloading = yes; // execute the background thread after the refresh starts. Before that, you can enable the HUD or block the UI [nsthread detachnewthreadselector: @ selector (doinbackground) totarget: Self withobject: Nil];} // method called when loading is completed-(void) doneloadingtableviewdata {nslog (@ "doneloadingtableviewdata"); _ reloading = no; [_ refreshtableview egorefreshscrollviewdatasourcedidfinishedloading: Self. tableview]; // refresh the table content [self. tableview reloaddata] ;}# Pragma mark-# pragma mark background operation // This method runs in the Child thread to complete the acquisition and refresh operations-(void) doinbackground {nslog (@ "doinbackground"); nsarray * dataarray2 = [nsarray arraywithobjects: @ "ryan2", @ "vivi2", nil]; self. array = dataarray2; [nsthread sleepfortimeinterval: 3: yes] ;}# Pragma mark-# pragma mark egorefreshtableheaderdelegate Methods // delegate method triggered by the drop-down-(void) delegate :( egorefreshtableheaderview *) view {[self reloadtableviewdatasource];} // returns the current refresh or refreshing status-(bool) egorefreshtableheaderperformanceisloading :( egorefreshtableheaderview *) view {return _ reloading;} // returns the callback method of the refresh time-(nsdate *) authorization :( egorefreshtableheaderview *) view {return [nsdate date];} # pragma mark-# pragma mark uiscrollviewdelegate Methods // delegate method of the Rolling Control-(void) scrollviewdidscroll :( uiscrollview *) scrollview {[_ refreshtableview progress: scrollview];}-(void) rollback :( uiscrollview *) scrollview willdecelerate :( bool) decelerate {[_ refreshtableview progress: scrollview];} @ end

The above has implemented the pull-down refresh function that is currently popular. The above is only a framework that meets the actual needs. You can add the function as needed.


Code: Download

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group

Welcome to my Sina Weibo chat: @ Tang Ren _ Ryan

Related Article

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.