tableView 展示汽车信息 加 索引栏

通过 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 文件结构





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值