#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
通过加载plist文件显示分组数据
最新推荐文章于 2023-04-13 14:25:09 发布