IOS Development Guide Chapter 5th Delegating Protocol Data Source Protocol Advanced View Learning

Source: Internet
Author: User

1 The delegate object is responsible for controlling the appearance of the control and reacting to the control's events and states

The data source object is a bridge between the control and the Application data (model), which is generally necessary.

2 selector Uipickerview gives users a choice

1) Date picker Uidatepicker

Set individual properties in the property Inspector-code

-setdateformat: Formatting dates

-stringfromdate: Get Time

-(Ibaction) onclick: (ID) Sender {NSDate* Thedate =self.datePicker.date; Gets the selected date NSLog (@"The date picked is:%@", [Thedate Descriptionwithlocale:[nslocale Currentlocale]]); Returns localization-based date information NSDateFormatter* Dateformatter =[[NSDateFormatter alloc] init]; date format Dateformatter.dateformat=@"YYYY-MM-DD HH:mm:ss"; -setdateformat:
NSLog (@"The date formate is:%@", [Dateformatter stringfromdate:thedate]); Self.label.text=[Dateformatter stringfromdate:thedate]; }

2) General selector Uipickerview Datesource and Delegate

**************

Viewdidload: To prepare the default (current display data), set the proxy

**************

Get data: Get home directory +mainbundle

Get Resource Path-pathforresource:oftype:

Get data-initwithcontentsoffile:                    

- (void) viewdidload{[Super Viewdidload]; NSBundle*bundle =[NSBundle Mainbundle]; NSString*plistpath = [Bundle Pathforresource:@"provinces_cities"OfType:@"plist"]; //get all the data in a property list fileNsdictionary *dict =[[Nsdictionary alloc] initwithcontentsoffile:plistpath]; Self.pickerdata=dict; //Province name DataSelf.pickerprovincesdata =[Self.pickerdata AllKeys]; fetch all key values from the dictionary//data for all municipalities in the first province are taken out by defaultNSString *seletedprovince = [Self.pickerprovincesdata objectatindex:0]; Self.pickercitiesdata=[Self.pickerdata objectforkey:seletedprovince]; Self.pickerView.dataSource=Self ; Self.pickerview.Delegate=Self ; }

*********

A key multiple value values can be placed in an array, and then a dictionary is formed.

Remove all key-Remove the first key-to remove the value corresponding to this key.

*********

Action method:

-selectedrowincomponent: Returns the number of rows for the selected dial

 -(ibaction) onclick: (id   = [self.pickerview selectedrowincomponent:0  ' Span style= "color: #000000;" >]; The number of rows in the first dial nsinteger row2  = [Self.pickerview selectedrowincomponent:1     ];    NSString  *selected1 = [Self.pickerprovincesdata Objectatindex:row1]; Take data based on number of rows        NSString  *selected2 = [Self.pickercitiesdata Objectatindex:row2]; NSString  *title = [[NSString alloc] Initwithformat:@ " %@,%@ City   "  Selected1,select        ED2];    Self.label.text  = title; }

Implementing the Proxy protocol:

DataSource:

-Numberofcomponentsinpickerview: Returns the number of dials

-pickerview:numberofrowsincomponent: Returns the number of rows per dial

Delegate

-pickerview:titleforrow:forcomponent: Provides display data to a row in a dial

-pickerview:didselectrow:incomponent: Called when the row is selected

-reloadcomponent: Refresh dial and callback display method

#pragmaMark implements the protocol Uipickerviewdatasource Method-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview {return 2;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component {if(Component = =0) {//Number of Provinces        return[Self.pickerprovincesdata Count]; } Else{//Number of cities        return[Self.pickercitiesdata Count]; }    }#pragmaMark implements the protocol Uipickerviewdelegate method-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component {if(Component = =0) {//Select Province Name        return[Self.pickerprovincesdata Objectatindex:row]; } Else{//Select city Name        return[Self.pickercitiesdata Objectatindex:row]; }}- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component {if(Component = =0) {NSString*seletedprovince =[Self.pickerprovincesdata Objectatindex:row]; Nsarray*array =[Self.pickerdata objectforkey:seletedprovince]; Self.pickercitiesdata=Array; [Self.pickerview reloadcomponent: 1 ]; This will callback the above display method}}

3 Collection view (grid view) Uicollectionview

Structure: section, cell (Uicollectionviewcell class, layout defined by Uicollectionviewlayout Class), supplemental view, decorative view, controller (Uicollectionviewcontroller)

Prepare: Add resource-delete viewcontroller-add Collectionviewcontroller to design interface and set to is Initial View controller-Modifying the Viewcontroller class to be a Uicollectionviewcontroller class and associating with the interface-adaptation screen

design and add cells : A cell is a view that allows you to place other views or controls inside it.

Create a new cell class and associate it with the collection View cell of the design interface-set a reusable cell label in the cell property Inspector-Set cell size in the cell Size Inspector-set collection cell in the View dimension Inspector Size value-Customize in Cell view-add constraint

@interface Custom Cell**label;

To implement a trust agreement:

DataSource:

-numberofsectionincollectionview: Number of knots

-collectionview:numberofiteminsection: Number of columns in a section

-collectionview:cellforitematindexpath: Providing display data for cells

-collectionview:viewforsupplementaryelementofkind:atindexpath: Providing display data for supplemental views

- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.NSBundle*bundle =[NSBundle Mainbundle]; NSString*plistpath = [Bundle Pathforresource:@"Events"OfType:@"plist"]; //get all the data in a property list fileNsarray *array =[[Nsarray alloc] initwithcontentsoffile:plistpath]; Self.events=Array; }#pragmamark-uicollectionviewdatasource-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return[Self.events Count]/2;}-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return 2;}-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{ reuse mechanism Cell*cell = [CollectionView dequeuereusablecellwithreuseidentifier:@"Cell"Forindexpath:indexpath]; Nsdictionary*Event= [self.events objectatindex: (indexpath.section*2+Indexpath.row)]; Cell.label.text= [EventObjectforkey:@"name"]; Cell.imageView.image= [UIImage imagenamed:[EventObjectforkey:@"Image"]]; returncell;}#pragmamark-uicollectionviewdelegate-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{nsdictionary*Event= [self.events objectatindex: (indexpath.section*2+Indexpath.row)]; NSLog (@"Select Event Name:%@", [EventObjectforkey:@"name"]);}

IOS Development Guide Chapter 5th Delegating Protocol Data Source Protocol Advanced View Learning

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.