#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSMutableArray *dataList;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSIndexPath *selectIP;//選中的索引
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// self.view.backgroundColor = [UIColor greenColor];
/**
* 從工程目錄中讀取文件
*/
// [NSBundle mainBundle]loadNibNamed:(NSString *) owner:(id) options:(NSDictionary *)
//1.獲取文件路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"font" ofType:@"plist"];
//2.通過路徑加載容器對象
_dataList = [NSMutableArray arrayWithArray:[NSArray arrayWithContentsOfFile:path]];
//TableView 的尺寸
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64) style:UITableViewStylePlain];
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
}
#pragma mark --UITableViewDelegate
//點擊單元格調用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"選中 %ld-%ld",indexPath.section,indexPath.row);
_selectIP = indexPath;//記錄點擊的單元格索引
[tableView reloadData];
//滑動到點擊位置
// [tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
//
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"上一個選中 %ld-%ld",indexPath.section,indexPath.row);
}
//指定單元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
//指定組的頭視圖高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50;
}
//指定組的尾視圖高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 50;
}
//設置組頭視圖
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 100)];
label.backgroundColor = [UIColor cyanColor];
label.text = [NSString stringWithFormat:@"section:%ld",section];
return label;
}
//設置組尾視圖 :注意 return UIView類型可以是任何繼承自UIView的類型
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
label.backgroundColor = [UIColor yellowColor];
label.text = [NSString stringWithFormat:@"section:%ld",section];
return label;
}
/*----------------------------------------------------------------------------------*/
//單元格即將顯示時調用 --> 單元格滑動到可視范圍內
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
}
//單元格結束顯示時調用 --> 單元格滑動到可視范圍外
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath{
}
//指定某一行的輔助視圖
- (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
if (indexPath == _selectIP) {
return UITableViewCellAccessoryCheckmark;
}
return UITableViewCellAccessoryNone;
}
#pragma mark --UITableViewDataSource
//可選方法:返回 組個數(shù)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return _dataList.count;//組個數(shù)
}
//返回 每個組有多少個單元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//1.從datalist中獲取section下標對應的對象--->小數(shù)組(盛放的NSString*)
NSArray *subArray = _dataList[section];
return subArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identy = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identy];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy];
}
NSArray *subArray = _dataList[indexPath.section];
//(2)在二級數(shù)組中 找到row對應的字符串對象
cell.textLabel.text = [NSString stringWithFormat:@"%ld-%ld %@",indexPath.section,indexPath.row,subArray[indexPath.row]];
return cell;
}
@end
屏幕快照 2016-03-03 下午7.56.09.png
屏幕快照 2016-03-03 下午7.50.23.png