Day.03.03 UITableView 表视图数据(分组)

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    
    NSArray *_dataList;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //分组情况下 创建表视图
    UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    //设置代理对象
    tableView.dataSource = self;
    
    tableView.delegate = self;
    
    [self.view addSubview:tableView];
    
    /**
     *  从工程目录中读取文件
     */
    
    //1.获取文件路径 bundle 获取工程目录的方法
                        //bundle 在括号里面打不出来 另换一行打 然后放在括号里面
    NSString *path = [[NSBundle mainBundle]pathForResource:@"font" ofType:@"plist"];
    
    //2.通过路径加载容器对象
    _dataList = [NSArray arrayWithContentsOfFile:path];
    
    NSLog(@"%@",_dataList);
    
}

#pragma mark --UITableViewDataSource

//可选方法:返回 组个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    //组个数
    return _dataList.count;
}

//返回 每个组有多少个单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //1.从datalist中获取section下标对应的对象--->小数组(盛放的NSString*)
    NSArray *subArray = _dataList[section];
    
    return subArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //1.
    static NSString *identifier = @"font_cell";
    
    //2.
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //3.
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    
    
    //1.不分组情况
    
    //    cell.textLabel.text = [_dataList objectAtIndex:indexPath.row];
    //
    //    cell.textLabel.font = [UIFont fontWithName:[_dataList objectAtIndex:indexPath.row] size:20];
    
    //2.分组
    
        //(1)在datalist中 找到section对应的二级数组
    
    NSArray *subArray = _dataList[indexPath.section];
    
        //(2)在二级数组中 找到row对应的字符串对象
    cell.textLabel.text = subArray[indexPath.row];
    cell.textLabel.font = [UIFont fontWithName:subArray[indexPath.row] size:20];
    
    return cell;
}

@end


屏幕快照 2016-03-03 上午9.55.19.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容