UITableView Realize the Automobile brand (demo) _ios

Source: Internet
Author: User

Look at the TableView data has been quite a long time, has been trying to write something, but always for a variety of reasons procrastination, this evening sometimes calm down to record some recent learning tableview knowledge. The following into the topic, UITableView can be called Uikit inside the most complex a control, the use of it is not difficult, but to use good is not easy. When used, we must take into account the design of background data, Tableviewcell design and reuse, as well as the efficiency of tableview and so on.

The last introduction of the UITableView, here to do a uitableview small program, car brand, screenshot as follows:

1.1 To create the project, here is not much to say.

1.2 Put all the pictures of the car brand into Images.xcassets, as shown below:

1.3 Create plist data, plist data inside each array for a car brand group, each array inside another array, which holds each packet under the brand car data, the data below.

1.4 After the creation of the data, and then design the page, the page is very simple, directly put a uitable view on it.

2.1 Background code, the first step to import

<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

Only by importing the agents of this uitable view can we use some of the corresponding methods of Uitable view in the following code.

2.2 Create the properties of the Uitable view control, and create an array of stored data, as follows.

@property (Weak, nonatomic) Iboutlet UITableView *tableview;
@property (Nonatomic,strong) Nsarray *cargroups;

2.3 Load data, this side first to create two model classes to save data, the country for us here is the data in the local plist culture, so we want to read this plist inside of the data to save in

Created in the cargroups array, and the local plist file is an array type, and there is an array in each array, so we're going to create two model classes to hold the data, a model class to hold the outer array data, A model class to hold the data of the child array inside the array, and then create and plist the properties and methods of the corresponding data within the model class.

The code is as follows:

#import <Foundation/Foundation.h> @interface zkcarmodel:nsobject//Avatar @property (nonatomic,copy) nsstring * icon;
The name @property (nonatomic,copy) NSString *name;
+ (Instancetype) carwithdict: (Nsdictionary *) dic;
-(Instancetype) Initwithdict: (Nsdictionary *) dic; @end #import "ZKCarModel.h" @implementation Zkcarmodel-(Instancetype) Initwithdict: (Nsdictionary *) dic {if (self=[
 Super Init]) {[Self setvaluesforkeyswithdictionary:dic];
return self; } + (Instancetype) carwithdict: (Nsdictionary *) dic {return [[Self alloc] initwithdict:dic]; @end #import <foundation/ foundation.h> #import "ZKCarModel.h" @interface zkcargroupmodel:nsobject//title @property (nonatomic,copy) NSString *
Title
@property (Nonatomic,strong) Nsarray *cars;
+ (Instancetype) Cargroupwithdic: (Nsdictionary *) dic;
-(Instancetype) Initwithdict: (Nsdictionary *) dic; @end #import "ZKCarGroupModel.h" @implementation Zkcargroupmodel-(Instancetype) Initwithdict: (Nsdictionary *) dic {if ( Self=[super Init]) {Self.title=dic[@ "title"];
 Nsmutablearray *array=[nsmutablearray Array];
 For (Nsdictionary *dict in dic[@ "Cars"]) {Zkcarmodel *car=[zkcarmodel carwithdict:dict];
 [Array Addobject:car];
 } Self.cars=array;
return self; } + (Instancetype) Cargroupwithdic: (Nsdictionary *) dic {return [[Self alloc] initwithdict:dic]; @end

2.4, the corresponding data of the model class is created, start creating the array lazy load

The code is as follows:

#import <Foundation/Foundation.h> @interface zkcarmodel:nsobject//Avatar @property (nonatomic,copy) nsstring * icon;
The name @property (nonatomic,copy) NSString *name;
+ (Instancetype) carwithdict: (Nsdictionary *) dic;
-(Instancetype) Initwithdict: (Nsdictionary *) dic; @end #import "ZKCarModel.h" @implementation Zkcarmodel-(Instancetype) Initwithdict: (Nsdictionary *) dic {if (self=[
 Super Init]) {[Self setvaluesforkeyswithdictionary:dic];
return self; } + (Instancetype) carwithdict: (Nsdictionary *) dic {return [[Self alloc] initwithdict:dic]; @end #import <foundation/ foundation.h> #import "ZKCarModel.h" @interface zkcargroupmodel:nsobject//title @property (nonatomic,copy) NSString *
Title
@property (Nonatomic,strong) Nsarray *cars;
+ (Instancetype) Cargroupwithdic: (Nsdictionary *) dic;
-(Instancetype) Initwithdict: (Nsdictionary *) dic; @end #import "ZKCarGroupModel.h" @implementation Zkcargroupmodel-(Instancetype) Initwithdict: (Nsdictionary *) dic {if ( Self=[super Init]) {Self.title=dic[@ "title"];
 Nsmutablearray *array=[nsmutablearray Array];
 For (Nsdictionary *dict in dic[@ "Cars"]) {Zkcarmodel *car=[zkcarmodel carwithdict:dict];
 [Array Addobject:car];
 } Self.cars=array;
return self; } + (Instancetype) Cargroupwithdic: (Nsdictionary *) dic {return [[Self alloc] initwithdict:dic]; @end

2.5, after the data loaded, and then start to write uitable view in the corresponding proxy method

The code is as follows:

/Set partition-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView {return
Self.carGroups.count;
 //Set How many rows of data each partition displays-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section {
 Zkcargroupmodel *model=self.cargroups[section];
return Model.cars.count;
 ///per-row display data-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) IndexPath {
 Static NSString *id=@ "A";
 Reads the cell UITableViewCell *cell=[tableview Dequeuereusablecellwithidentifier:id] from the cache; If there is no cell in the cache, create a new cell if (cell==nil) {Cell=[[uitableviewcell alloc] Initwithstyle:uitableviewcellstylesubtitle
 Reuseidentifier:id];
 //Find the index of the current partition Zkcargroupmodel *groupmodel=self.cargroups[indexpath.section];
 Locate the row Zkcarmodel *carmodel=groupmodel.cars[indexpath.row] for the current partition;
 Set the text cell.textlabel.text=carmodel.name for the cell display;
 Set the cell display picture cell.imageview.image=[uiimage ImageNamed:CarModel.icon];
return cell; }

The above 3 proxy methods are the 3 most commonly used methods in uitable View . After writing these 3 methods to run Xcode you can see the data.

But there are some small problems here, all the brands shown here are from the top down, not a group, so we want to find which brand of the car is not very easy, so we have the same data of the car brand with a letter to indicate how this is done, this will give uitable view of each partition add a head , using the titleforheaderinsection proxy method

The code is as follows:

Set Header style
-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section
{
 Finds the index of the current partition in the array
 Zkcargroupmodel *model=self.cargroups[section];
 
 Returns the title return Model.title in the current partition's data
 ;

2.6 Above the program , in the screen at the far right there is an index, point this index to find the corresponding partition data, in fact this is also very simple, but also call a

A Sectionindextitlesfortableview proxy method that returns an array of arrays.

The code is as follows:

Set Index
-(Nsarray *) Sectionindextitlesfortableview: (UITableView *) TableView
{return
 [ Self.cargroups valueforkeypath:@ "title"];

2.7, this program also made a , when you click on the screen each car brand will also pop up a dialog box, why do this, because many times the screen picture and text are clickable, so the light to do a static display is not very good, This dialog doesn't seem to be working, but it's just about the use of this method.

The code is as follows:

Click cell time Change
-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath
{
 //Create a dialog box
 Uialertview *alertview=[[uialertview alloc] initwithtitle:@ "Auto" message:@ "Cancel" delegate:self cancelbuttontitle:@ "confirm" otherbuttontitles:@ "cancellation", nil];
 Set style
 alertview.tag=1;
 Alertview.alertviewstyle=uitableviewcellstylesubtitle;
 [Alertview];
 
 [Alertview show];
}

3.1 A UITableView car brand is OK, although this is not an app but, here has uitableview some of the common agent methods are written, of course, UITableView there are many representative methods, this is not said, but after these, In the future use we can come back to the query, the important thing is thinking.

The above is UITableView realize all the contents of the automobile brand, hope to help everyone.

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.