tableviewcontroller-City List of iOS advanced controls

Source: Internet
Author: User

Today we are going to do is a city information display, first show you how an effect, I think it should be absolutely practical ^_^

OK, then I will briefly tell you about the steps, because the technical blog is not really much written, do not know how to speak to everyone can understand, but the baby I have the spirit of writing a blog, as far as possible to describe the details of the detailed, the wrong place, look.

haha although feel and not many people look, I think I should be their loyal fans ^_^ good not to say nonsense, into the topic

First, create a new project, then delete the Viewcontroller in storyboard, and delete the left viewController.h and. M together, as this is a demo of a proprietary control, so avoid a bunch of complicated code ^_^

At the same time, new OC class Maintableviewcontroller, remember to inherit to Uitableviewcontroller, and then create a new Citiestableviewcontroller to store the display city information.

Then storyboard the table view controller into the project and set the class of the table view Controller to Maintableviewcontroller so that this class and the table view The controller is connected. Then remember to choose this, this is the mobile phone at the time of operation, the first display screen, equivalent to initialize, otherwise it is black screen.

At the same time provinces and cities before the plist all pull into the project, here will not repeat.

Define, and at the same time initialize the data, these are the old routines

//Province array   @property  (strong , nonatomic ) nsarray  *provinces; //City dictionary   @property  (strong , nonatomic ) nsdictionary  *cities;  
- (void)viewDidLoad {[super viewDidLoad];    //初始化数据NSBundle *bundle = [NSBundle mainBundle];self.provinces = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"provinces" ofType:@"plist"]];self.cities = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"cities" ofType:@"plist"]];}

Then you set the data source.

#pragma mark 指定section分组的数据行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    // Return the number of rows in the section.    return self.provinces.count;}

And we're going to set what's shown in the cell.

#pragma mark specifies the table to display content- (UITableViewCell*) TableView: (UITableView*) TableView Cellforrowatindexpath: (Nsindexpath*) Indexpath {Static NSString*cellidentifier = @"MyCell";UITableViewCell*cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];//If no cell is found in the cache pool, instantiate a new cellif(Cell = =Nil) {cell = [[UITableViewCellAlloc]initwithstyle:uitableviewcellstyledefault Reuseidentifier:cellidentifier];}//Configure the cell ...[Cell. TextlabelSetText: Self. Provinces[Indexpath. Row]];returnCell;}

Then we need to know, when we click on which line, and then through this information in the city dictionary to the province of the array out, here to do the connection preparation, and we want to create a new Tableviewcontroller in the storyboard, and associate the class with the Citiestableviewcontroller.

By the way, this setcities method is to be defined in the Citiestableviewcontroller, the baby side does not repeat said, so we understand. Remember import header file

Then we write citiestableviewcontroller.m, it is very simple, the other useless are removed, we only have 2 effects, one is to return how many rows, and the other is in the return of these lines display what content. The code is almost the same as before.

//返回的行数- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    returnself.cities.count;}


Everyone must be on the above @ "Mycity" and before the @ "MyCell" confusion, this is where, this is actually a detail, we open the storyboard, under the Tableviewcontroller there is a table view, Then there is a cell, where the cell is labeled with a label, which is equivalent to the name.

Only this certainly is not possible, this will encounter a very embarrassing scene, that is, click into a province, the results can not be returned, haha

At this point, you need another control, Navigation the Controller, drag it into the storyboard, and it will be accompanied by many viewcontroller delete, in the property will start the item is initial view Controller tick, At this time originally in the province tableviewcontroller front of the shoulder ran to the navigation Controller

At the same time will navigation controller and Maintableviewcontroller connected together, here will not repeat, give everyone a hint to do it yourself, as long as it is the root view controller, At the same time the connection between the province and the city, select the push below the accessory action event, which will still appear in the city list
Navigation Controller

Some other landscaping and naming operations will no longer be introduced, I believe this demo will be able to learn in the way you are helpful ^_^

Write the attitude of the blog, do have feelings of people!

tableviewcontroller-City List of iOS advanced controls

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.