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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容