////VIEWCONTROLLER.M//zqrtableviewtest////Created by Zzqqrr on 17/8/24.//Copyright (c) 2017 zzqqrr. All rights reserved.//#import "ViewController.h"#import "ZQRCarGroup.h"@interfaceViewcontroller () <UITableViewDataSource>@property (weak,nonatomic) iboutlet UITableView*TableView, @property (nonatomic,strong) Nsarray*cargroups;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //set up a data sourceSelf.tableview.datasource=Self ;}-(Nsarray *) cargroups{if(_cargroups==Nil) {Zqrcargroup*model1=[[Zqrcargroup alloc] init]; Model1.title=@"haha"; Model1.desc=@"hehe"; Model1.cars[Email protected] [@"First group first row"]; Zqrcargroup*model2=[[Zqrcargroup alloc] init]; Model2.title=@"haha"; Model2.desc=@"hehe"; Model2.cars[Email protected] [@"first row of the second group",@"Second group second row"]; Zqrcargroup*model3=[[Zqrcargroup alloc] init]; Model3.title=@"haha"; Model3.desc=@"hehe"; Model3.cars[Email protected] [@"The first line of the third group",@"Third group, second row",@"Third group, third row"]; _cargroups=@[model1,model2,model3]; } return_cargroups;}/** Set How many group groups*/-(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{returnSelf.carGroups.count;}/** Specify rows in a group*/-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{Zqrcargroup*group=Self.cargroups[section]; returnGroup.cars.count;}/** Content displayed for each group*/-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{UITableViewCell*cell=[[UITableViewCell alloc] Initwithstyle:uitableviewcellstyledefault Reuseidentifier:nil]; Zqrcargroup*group=Self.cargroups[indexpath.section]; NSString*strcar=Group.cars[indexpath.row]; Cell.textLabel.text=Strcar; returncell;}/** Head Text*/-(NSString *) TableView: (UITableView *) TableView titleforheaderinsection: (nsinteger) section{Zqrcargroup*model=Self.cargroups[section]; returnModel.title;}/** Trailing text*/-(NSString *) TableView: (UITableView *) TableView titleforfooterinsection: (nsinteger) section{Zqrcargroup*model=Self.cargroups[section]; returnModel.desc;}@end
Model
////ZQRCarGroup.h// ////Created by Zzqqrr on 17/8/25.////#import<Foundation/Foundation.h>@interfaceZqrcargroup:nsobject/** * Head title*/@property (nonatomic,copy) NSString*title;/** Description of the tail*/@property (nonatomic,copy) NSString*desc;/** * All information list*/@property (nonatomic,strong) Nsarray*cars;@end
// // zqrcargroup.m// // // Created by Zzqqrr on 17/8/25. // // #import " ZQRCarGroup.h " @interface Zqrcargroup () @end @implementation Zqrcargroup @end
iOS primary knowledge UITableView and Simple Usage II (model data)