TableViewController-city list Analysis of iOS advanced controls

Source: Internet
Author: User

TableViewController-city list Analysis of iOS advanced controls

Today we are going to present the information of a city. Let's show you how it works. I think it should be absolutely practical. ^_^

Now, let me give you a brief description of the steps, because the technical blog does not really write much, and I don't know how to explain it so that everyone can understand it, however, I write a blog with an attitude, and try to describe the details in detail. If not, I hope to correct it.

Haha, although I don't think many people have read it, I think I should be a loyal fan of myself.

First, create a new project, delete the viewController In the storyboard, and delete the viewController on the left. h and. m is deleted together, because this is a demo of a proprietary control, so avoid the emergence of a series of complicated code ^_^

At the same time, create an oc-like MainTableViewController. Remember to inherit to UITableViewController, and create a new CitiesTableViewController to store information about the display city.

In the storyboard, drag Table View Controller into the project and set the class of Table View Controller to MainTableViewController. In this way, this class is associated with this Table view controller. Remember to choose this one. This is the first screen displayed when the mobile phone is running, which is equivalent to initialization. Otherwise, the screen is black.

At the same time, we will pull all the plist of provinces and cities into the project, so we will not go into detail here.

Define and initialize the data at the same time.

// Province array @ property (strong, nonatomic) NSArray * provinces; // city dictionary @ property (strong, nonatomic) NSDictionary * cities;
-(Void) viewDidLoad {[super viewDidLoad]; // initialize the data NSBundle * bundle = [NSBundle mainBundle]; self. provinces = [NSArray arrayWithContentsOfFile: [bundle pathForResource: @ "provinces" ofType: @ "plist"]; self. cities = [NSDictionary dictionaryWithContentsOfFile: [bundle pathForResource: @ "cities" ofType: @ "plist"];}

The next step is to set the data source.

# Pragma mark specifies the number of data rows in the section Group-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {// Return the number of rows in the section. return self. provinces. count ;}

Then we need to set what is displayed in the cell.

# Pragma mark specifies the table 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 cell if (cell = nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellIdentifier];} // Configure the cell... [cell. textLabel setText: self. provinces [indexPath. row]; return cell ;}

Then, we need to know which row we click and use this information to retrieve the province array in the city dictionary. We need to prepare the link here. At the same time, we need to create a tableviewcontroller In the storyboard, and associate the class with the citiestableviewcontroller.

By the way, this setCities method should be defined in the citiestableviewcontroller. The baby will not repeat it here, so everyone understands. Remember impZ finished? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcnTNt87EvP48L3A + large + 8O708O1xLa8yb6z/large/D5s/large" brush: java; ">// The number of returned rows-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section {return self. cities. count ;}


Everyone must be confused about the above @ "myCity" and the previous @ "myCell". Where did this come from? This is actually a detail. Let's open the storyboard, there is a table view under tableviewcontroller, and there is a cell below. The function of marking a cell is equivalent to the same name.

This is definitely not the case. In this case, we will encounter a very embarrassing situation, that is, we click to enter a certain province, and the result cannot be returned. Haha

In this case, another control, Navigation Controller, Is required to drag it into the storyboard and delete the multiple viewcontrollers that accompany it. In the attribute, the startup Item Is initial view controller, at this time, the shoulder in front of the tableviewcontroller in the Province ran to the Navigation Controller.

At the same time, we will connect the Navigation Controller and MaintableviewController. Here we will not go into details. It is very easy to remind everyone to do it by themselves, as long as it is the root view controller and the connection between provinces and cities, select the push under the Accessory action event, which will still appear in the city list
Navigation Controller

Other beautification and naming operations will not be introduced. I believe this demo will certainly help you on your way to learning ^_^

Write an attitude blog and be a sentimental person!

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.