多组表格的思路

多组表格的思路
一、分析plist文件中的形式,创建model层数据 NSArray —> NSDictionary —>NSArray

最外层的数组
+ ( instancetype )carGroupsWithDict:( NSDictionary  *)dict
{
    
return  [[ self   alloc initWithDict :dict];
}

- (
instancetype )initWithDict:( NSDictionary  *)dict
{
    
self  = [ super   init ];
    [
self   setValue :dict[ @"title" forKey : @"title" ];
    
self . carGroups  = [ SUNCar   carsWithArray :dict[ @"cars" ]];
    
return   self ;
}

+ (
NSArray  *)carGroups
{
    
NSArray  *array = [ NSArray   arrayWithContentsOfFile :[[ NSBundle   mainBundle pathForResource : @"cars_total.plist"   ofType : nil ]];
    
NSMutableArray  *arrayM = [ NSMutableArray   array ];
    
for  ( NSDictionary  *dict  in  array) {
        [arrayM 
addObject :[ self   carGroupsWithDict :dict]];
    }
    
return  arrayM;
}

最里层的数组
+ ( instancetype )carWithDict:( NSDictionary  *)dict
{
    
return  [[ self   alloc initWithDict :dict];
}

- (
instancetype )initWithDict:( NSDictionary  *)dict
{
    
self  = [ super   init ];
    [
self   setValuesForKeysWithDictionary :dict];
    
return   self ;
}

+ (
NSArray  *)carsWithArray:( NSArray  *)array
{
    
NSMutableArray  *arrayM = [ NSMutableArray   array ];
    
for  ( NSDictionary  *dict  in  array) {
        [arrayM 
addObject :[ self   carWithDict :dict]];
    }
         
return  arrayM;
}

二、懒加载 UITableView
- ( UITableView *)tableView
{
   
if ( _tableView == nil ) {
       
_tableView = [[ UITableView alloc ] initWithFrame : self . view . bounds style : UITableViewStylePlain ];
       
_tableView . dataSource = self ;
       
        [
self . view addSubview : _tableView ];
    }
   
return _tableView ;
}
三、实现代理方法
#pragma mark - 数据源方法
// 分组总数
- (
NSInteger )numberOfSectionsInTableView:( UITableView *)tableView
{
   
return self . carGroups . count ;
}

// 每一组的总数
- (
NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section
{
   
HMCarGroup *group = self . carGroups [section];
   
   
return group. cars . count ;
}

// 单元格
- (
UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath
{
   
// 可重用标示符
   
static NSString *ID = @"Cell" ;
   
   
// 让表格缓冲区查找可重用 cell
   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier :ID];
   
   
// 如果没有找到可重用 cell
   
if (cell == nil ) {
       
// 实例化 cell
        cell = [[
UITableViewCell alloc ] initWithStyle : UITableViewCellStyleDefault reuseIdentifier :ID];
    }
   
   
// 设置 cell 内容
   
// 1> 取出数据模型
   
HMCarGroup *group = self . carGroups [indexPath. section ];
   
HMCar *car = group. cars [indexPath. row ];
   
   
// 2> 设置数据
    cell.
imageView . image = [ UIImage imageNamed :car. icon ];
    cell.
textLabel . text = car. name ;
   
   
return cell;
}

// 标题
- (
NSString *)tableView:( UITableView *)tableView titleForHeaderInSection:( NSInteger )section
{
   
// 找到 group
   
HMCarGroup *group = self . carGroups [section];
   
   
return group. title ;
}

// 右侧索引列表
- (
NSArray *)sectionIndexTitlesForTableView:( UITableView *)tableView
{

   
// 返回 self.carGroup title 的数组
//    NSMutableArray *arrayM = [NSMutableArray array];
//    for (HMCarGroup *group in self.carGroups) {
//        [arrayM addObject:group.title];
//    }
//    return arrayM;
   
   
// KVC cocoa 的大招
   
// 用来间接获取或者修改对象属性的方式
   
// 使用 KVC 在获取数值时,如果指定对象不包含 keyPath " 键名 " ,会自动进入对象的内部查找
   
// 如果取值的对象是一个数组,同样返回一个数组
    
//  NSArray *array = [self.carGroups valueForKeyPath:@"cars.name"];
    return [ self . carGroups valueForKeyPath : @"title" ];
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值