iOS Seventh day (5:uitableview car brand, complex model group show, A-Z request list) (2015-08-05-14:03)

Source: Internet
Author: User

Group presentation of complex models

#import "HMViewController.h"#import "HMCarGroup.h"#import "HMCar.h"@interfaceHmviewcontroller () <UITableViewDataSource>@property (nonatomic, strong) Nsarray*cargroups, @property (nonatomic, strong) UITableView*TableView;@end@implementationHmviewcontroller-(UITableView *) tableview{if(_tableview = =Nil) {_tableview=[[UITableView alloc] InitWithFrame:self.view.bounds Style:uitableviewstyleplain]; _tableview.datasource=Self ;    [Self.view Addsubview:_tableview]; }    return_tableview;}-(Nsarray *) cargroups{if(_cargroups = =Nil) {_cargroups=[Hmcargroup cargroups]; }    return_cargroups;}- (void) viewdidload{[Super Viewdidload]; //call TableView Add to view[self TableView];}#pragmaMark-Data Source methods//total number of groupings-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSelf.carGroups.count;}//total for each group-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Hmcargroup*group =Self.cargroups[section]; returnGroup.cars.count;}//Cell Cell-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{//Reusable Identifiers    StaticNSString *id =@"Cell"; //Let table buffers find reusable cellsUITableViewCell *cell =[TableView Dequeuereusablecellwithidentifier:id]; //If you do not find a reusable cell    if(Cell = =Nil) {        //Instantiate cellCell =[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:id]; }        //Set cell contents//1> extracting the data modelHmcargroup *group =Self.cargroups[indexpath.section]; Hmcar*car =Group.cars[indexpath.row]; //2> setting up dataCell.imageView.image =[UIImage ImageNamed:car.icon]; Cell.textLabel.text=Car.name; returncell;}//title-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{//Find GroupHmcargroup *group =Self.cargroups[section]; returnGroup.title;}//Right Index list-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) tableview{//"Content" in an indexed array, independent of grouping//Subscript in an indexed array, corresponding to a grouped subscript//return @[@ "wow", @ "Hello", @ "wow haha", @ "Hello", @ "wow haha", @ "Hello" @ "wow haha" @ "Hello"]; //returns an array of title in Self.cargroup//Nsmutablearray *arraym = [Nsmutablearray array];//For (Hmcargroup *group in self.cargroups) {//[Arraym AddObject:group.title];//    }//return Arraym; //KVC is Cocoa's big trick.//ways to indirectly get or modify the properties of an object//using KVC to get a numeric value, if the specified object does not contain KeyPath's "key Name", it automatically enters the object's internal lookup//if the object being evaluated is an array, it also returns an arrayNsarray *array = [self.cargroups valueforkeypath:@"Cars.name"]; NSLog (@"%@", array); return[Self.cargroups Valueforkeypath:@"title"];}@end

iOS Seventh day (5:uitableview car brand, complex model group show, A-Z request list) (2015-08-05-14:03)

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.