通过加载plist文件显示分组数据


#import "ViewController.h"
#import "GZGroup.h"
@interface ViewController ()<UITableViewDataSource>



@property (weak, nonatomic) IBOutlet UITableView *tableView;


//懒加载需要属性
@property(nonatomic,strong)NSArray *groups;

@end

@implementation ViewController

#pragma mark - 懒加载数据
-(NSArray *)groups{
   
    if(_groups == nil)
    {
        //懒加载数据
        //1、获取plist路径
        NSString *path = [[NSBundle mainBundle]pathForResource:@"sticker.plist" ofType:nil];
        //2、加载plist文件
        //NSArray: *arrayDict = [[NSArray alloc] initWithContentsOfFile:path];
//    NSString:NSString *arrayDict = [[NSBundle mainBundle] pathForResource:@"address" ofType:@"plist"];
      //当数据结构为数组时
    NSArray *arrayDict = [[NSArray alloc] initWithContentsOfFile:path];
        
        
        //3、把字典转换成模型
        NSMutableArray *arrayModel = [NSMutableArray array];

        //4、遍历字典数组中的每一个字典,把每个字典转换层模型,把模型放到arrayModel数组中
        for(NSDictionary *dict in arrayDict){
            //创建模型对象
            GZGroup *model = [GZGroup groupWithDict:dict];
        [arrayModel addObject:model];
        }
        _groups = arrayModel;

    }
    return _groups;

}


#pragma mark -数据源方法

//根据组索引(sectionh)获取组对象
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

//告诉UITableView每组显示几条(几行)数据
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //根据不同的组,返回每组显示不同条数的数据
    GZGroup *group = self.groups[section];
    return _groups.;
}


//每组的每行显示什么样的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //1、获取模型数据
    //获取组模型
    GZGroup *group = self.groups[indexPath.section];
   

    //创建单元格UITablViewCell
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    
    //3、把模型中的数据设置给单元格中的子控件
    cell.textLabel.text = group;
    //4、返回单元格UITableViewCell
    return cell;
}


//每一组的组标题显示什么
-(NSString *)tableView:(UITableView*)tableView tetleForHeaderInsection:(NSInteger)section{
    //根据当前组的索引section,返回不同组的标题

    GZGroup *group = self.groups[section];
    return group.name;
}
//每一组的组尾即组描述
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    //根据当前组的索引section,返回不同组的描述信息

    GZGroup *group = self.groups[section];
    return group.description;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Αиcíеиτеǎг

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值