iOS 设置tableviewView组的快速检索

@interface ViewController ()<UITableViewDataSource>


@property (nonatomic,strong) NSArray *dataArray;


@property (nonatomic,strong) NSArray *indexArray;//检索的索引数组


@end


@implementation ViewController


#pragma mark -

#pragma mark -  懒加载

- (NSArray *)dataArray {

    if (nil ==_dataArray) {

        

        // 1. 路径

        NSString *path = [[NSBundlemainBundle] pathForResource:@"cars_total.plist"ofType:nil];

        

        // 2. 读取

        NSArray *tempArray = [NSArrayarrayWithContentsOfFile:path];

        

        // 3. 临时可变数组

        NSMutableArray *mutal = [NSMutableArrayarray];

        

        // 4.

        for (NSDictionary *dictin tempArray) {

            CarModel *model = [CarModelcarModelWithDict: dict];

            

            [mutal addObject:model];

        }

        

        // carModeltitle放入数组, 用作后面的索引返回值

        _indexArray = [mutal valueForKeyPath:@"title"];

        

        _dataArray = mutal;

    }

    return_dataArray;

}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

}



// 多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;

}


// 多少行

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    CarModel *carModel = self.dataArray[section];

    

    return carModel.cars.count;

}


// 每一行显示的内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    // 1. 定义一个重用标识符

    static NSString *identifier =@"settingCell";

    

    // 2. 到缓存池中去找对应的cell根据重用标识符

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    

    // 3. 判断是否找到,如果找不到, 就重新实例化cell

    if (nil == cell) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identifier];

    }

    

    // 4. 设置数据

    // 先取出该组对应的model

    CarModel *carModel = self.dataArray[indexPath.section];

    

    // 再取出该组中行对应的model

    InnerCarModel *innerCarModel = carModel.cars[indexPath.row];

    

    // cell的控件进行赋值

    cell.imageView.image = [UIImageimageNamed:innerCarModel.icon];

    

    cell.textLabel.text = innerCarModel.name;

    

    return cell;

}

//设置组头标题

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    // 取出carModel

    CarModel *carModel = self.dataArray[section];

    

    return carModel.title;

}


//设置组的快速搜索

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    

    return_indexArray; 

}


//隐藏状态栏

- (BOOL)prefersStatusBarHidden {

    return YES;

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值