ViewController UITableView, viewcontroller

Source: Internet
Author: User

ViewController UITableView, viewcontroller

//

// ViewController. m

// CarDemo

//

// Created by GuoYule on 15/1/20.

// Copyright (c) 2015 GuoYule. All rights reserved.

//


# Import "ViewController. h"

# Import "CarGroup. h"

# Import "GYLcar. h"

@ Interface ViewController () <UITableViewDataSource>

/**

* Vehicle brand data

*/

@ Property (nonatomic, strong) NSArray * carGroups;

@ End


@ Implementation ViewController


-(Void) viewDidLoad {

[SuperviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}


-(BOOL) prefersStatusBarHidden

{

Return YES;

}

-(NSArray *) carGroups


{

If (_ carGroups = nil ){

// Initialization

// 1. Obtain the full path of plist

NSString * path = [[NSBundlemainBundle] pathForResource: @ "cars_total.plist" ofType: nil];

// 2. Load the Array

NSArray * dictArray = [NSArrayarrayWithContentsOfFile: path];

// 3. convert all the dictionaries in dictArray into model objects and place them in the new array.

NSMutableArray * carGroupsArray = [NSMutableArrayarray];

For (NSDictionary * dictin dictArray ){

// 3. 1. Create a model object

CarGroup * group = [CarGroupgroupWithDict: dict];

//. Add the model object to the array

[CarGroupsArrayaddObject: group];

}

// 4. assign a value

_ CarGroups = carGroupsArray;

}

Return_carGroups;

}

# Pragma mark-Data Source Model

-(NSInteger) numberOfSectionsInTableView :( UITableView *) tableView

{

Return self. carGroups. count;

}

-(NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section

{

CarGroup * group = self. carGroups [section];

Return group. cars. count;

}



-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath

{

// Define a loop ID

Static NSString * ID = @ "car ";

// 2. Fetch reusable cells from the cache pool

UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier: ID];

// 3. If the cache pool does not exist, create a new one and assign the ID to it.

If (cell = nil)

{

Cell = [[UITableViewCellalloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: ID];

}

// 4. Set Data

CarGroup * group = self. carGroups [indexPath. section];

GYLcar * car = group. cars [indexPath. row];

Cell. imageView. image = [UIImageimageNamed: car. icon];

Cell. textLabel. text = car. name;

Return cell;

}

/**

* Header title displayed in section group


*/

-(NSString *) tableView :( UITableView *) tableView titleForHeaderInSection :( NSInteger) section

{

CarGroup * group = self. carGroups [section];

Return group. title;

}

/**

* Returns the string data displayed in the right index hop.

*/

-(NSArray *) sectionIndexTitlesForTableView :( UITableView *) tableView {

Return [self. carGroupsvalueForKeyPath: @ "title"]; // This is an indirect value that KVC can take.

// Why not use [self. carGroups valueForKey: @ "title"] because this value is not indirect

}


@ End



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.