[IOS笔记]UITableView及索引条

源码下载

http://download.csdn.net/download/mashang123456789/9978766



//
//  ViewController.m
//  索引条1
//
//  Created by cdj on 17/9/13.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "ViewController.h"
#import "UECarGroup.h"
#import "UECar.h"


@interface ViewController ()

@property (nonatomic, strong)NSArray *carGroups;

@end

@implementation ViewController

-(NSArray *)carGroups{
    if(!_carGroups){
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cars.plist" ofType:nil] ];
        
        NSMutableArray *temp = [NSMutableArray array];
        for (NSDictionary *carGroupDict in dictArray) {
            [temp addObject:[UECarGroup carGroupWithDict:carGroupDict]];
        }
        
        _carGroups = temp;
    }
    return _carGroups;

}




- (void)viewDidLoad {
    [super viewDidLoad];

    //设置索引的文字颜色
    self.tableView.sectionIndexColor = [UIColor redColor];
    //设置索引的背景颜色
    self.tableView.sectionIndexBackgroundColor = [UIColor yellowColor];
    
}

//隐藏状态栏
-(BOOL)prefersStatusBarHidden{
    return YES;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.carGroups.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    UECarGroup *group = self.carGroups[section];
    return group.cars.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"car";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    UECarGroup *group = self.carGroups[indexPath.section];
    UECar *car = group.cars[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:car.icon];
    cell.textLabel.text = car.name;
    
    
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    UECarGroup *group = self.carGroups[section];
    return group.title;
}

/**
 *  添加索引
 *
 *  @param tableView <#tableView description#>
 *
 *  @return <#return value description#>
 */
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
   /*
    NSMutableArray *titles = [NSMutableArray array];
    for (UECarGroup *group in self.carGroups) {
        [titles addObject:group];
    }

    return titles;
        */
    
    return [self.carGroups valueForKey:@"title"];
}


@end


//
//  UECarGroup.h
//  索引条1
//
//  Created by cdj on 17/9/14.
//  Copyright © 2017年 ue. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UECarGroup : NSObject

@property(nonatomic, strong)NSMutableArray *cars;
@property(nonatomic, strong)NSString *title;

+(instancetype)carGroupWithDict:(NSDictionary *)dict;

@end

//
//  UECarGroup.m
//  索引条1
//
//  Created by cdj on 17/9/14.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "UECarGroup.h"
#import "UECar.h"

@implementation UECarGroup

+(instancetype)carGroupWithDict:(NSDictionary *)dict{

    UECarGroup *group = [[self alloc] init];
    group.title = dict[@"title"];
    
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *carDict in dict[@"cars"]) {
        [temp addObject:[UECar carWithDict:carDict]];
    }
    group.cars = temp;
    
    return group;
}

@end

//
//  UECar.h
//  索引条1
//
//  Created by cdj on 17/9/14.
//  Copyright © 2017年 ue. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface UECar : NSObject

@property(nonatomic, copy)NSString *icon;

@property(nonatomic, copy)NSString *name;

+(instancetype)carWithDict:(NSDictionary *)dict;

@end

//
//  UECar.m
//  索引条1
//
//  Created by cdj on 17/9/14.
//  Copyright © 2017年 ue. All rights reserved.
//

#import "UECar.h"

@implementation UECar

+(instancetype) carWithDict:(NSDictionary *)dict{
    UECar *car = [[self alloc] init];
    [car setValuesForKeysWithDictionary:dict];
    return car;
}

@end
























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值