通过 storyboard 与代码混合写的汽车展示的 tableView ,右侧有索引栏
TableViewController 和 cell 是通过 storyboard 建立
#import "TableViewController.h"
#import "CarsModel.h"
#import "CarCell.h"
#import "GroupModel.h"
@interface TableViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSArray * allcars;
@end
@implementation TableViewController
//懒加载数据
-(NSArray *)allcars
{
if (_allcars ==nil) {
NSString * path = [[NSBundlemainBundle]pathForResource:@"car.plist"ofType:nil];
NSArray * array = [NSArrayarrayWithContentsOfFile:path];
NSMutableArray * arr = [NSMutableArrayarray];
for (NSDictionary * ain array) {
GroupModel * cars = [GroupModelgroupModel:a];
[arr addObject:cars];
}
_allcars = arr;
}
return_allcars;
}
-(BOOL)prefersStatusBarHidden
{
returnYES;
}
//设置每个 section的标题
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
GroupModel * model =self.allcars[section];
return model.title;
}
- (void)viewDidLoad {
[superviewDidLoad];
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
//设置 section数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
returnself.allcars.count;
}
//设置每个 section的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
GroupModel * group =self.allcars[section];
return group.cars.count;
}
//设置每个 cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
GroupModel * group =self.allcars[indexPath.section];
CarsModel * cMod = group.cars[indexPath.row];
CarCell *cell = [tableViewdequeueReusableCellWithIdentifier:NSStringFromClass([cModclass])forIndexPath:indexPath];
[cell setInfo:cMod];
return cell;
}
//设置索引栏
-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSMutableArray * arr = [NSMutableArrayarray];
for (GroupModel * modelinself.allcars) {
[arr addObject:model.title];
}
return arr;
}
@end
//以下是 cell 文件
#import <UIKit/UIKit.h>
#import "CarsModel.h"
@interface CarCell : UITableViewCell
-(void)setInfo:(CarsModel *)car;
@end
#import "CarCell.h"
@interface CarCell ()
@property (weak,nonatomic)IBOutletUIImageView *img;
@property (weak,nonatomic)IBOutletUILabel *title;
@end
@implementation CarCell
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[supersetSelected:selectedanimated:animated];
// Configure the view for the selected state
}
-(void)setInfo:(CarsModel*)car
{
self.img.image = [UIImageimageNamed:car.pic];
self.title.text = car.name;
}
@end
//以下是模型
//内层 CarsModel
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CarsModel : NSObject
@property(nonatomic,strong)NSString * name;
@property(nonatomic,strong)NSString * pic;
-(instancetype)initWith:(NSDictionary *)dict;
+(instancetype)carsmodel:(NSDictionary *)dict;
@end
#import "CarsModel.h"
@implementation CarsModel
- (instancetype)initWith:(NSDictionary *)dict
{
self = [superinit];
if (self) {
[selfsetValuesForKeysWithDictionary:dict];
}
returnself;
}
+(instancetype)carsmodel:(NSDictionary *)dict
{
return [[selfalloc]initWith:dict];
}
@end
//外层 GroupModel
#import <Foundation/Foundation.h>
@interface GroupModel : NSObject
@property(nonatomic,strong)NSString * title;
@property(nonatomic,strong)NSArray * cars;
-(instancetype)initWith:(NSDictionary *)dict;
+(instancetype)groupModel:(NSDictionary *)dict;
@end
#import "GroupModel.h"
#import "CarsModel.h"
@implementation GroupModel
- (instancetype)initWith:(NSDictionary *)dict
{
self = [superinit];
if (self) {
[selfsetValuesForKeysWithDictionary:dict];
NSMutableArray * arrayModels = [NSMutableArrayarray];
for (NSDictionary * item_dictin dict[@"cars"]) {
CarsModel * model = [CarsModelcarsmodel:item_dict];
[arrayModels addObject:model];
}
self.cars = arrayModels;
}
returnself;
}
+(instancetype)groupModel:(NSDictionary *)dict
{
return [[selfalloc]initWith:dict];
}
@end
// storyboard
//要记得绑定 class 和 identifier
// plist 文件结构