UITableView表视图:
separatorColor : 分割线的颜色
separatorStyle : 分割线的样式
rowHeight : 设置行高
dataSource : 设置数据源(这也是一种代理方法)
delegate : 代理
tableFooterView : 设置页脚
[tableView
registerClass
:[
UITableViewCell
class
]
forCellReuseIdentifier
:
systemCellReuseIdentifier
];
//
最好定义为静态的全局变量
,
不会出错
遵循协议:设置数据源必须实现两个方法:
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath;
//
注册
cell:
如果
tableView
需要绘制
cell,
会根据注册时给定的类型还有对应的标识符来完成
cell
的绘制
,
并且在重用池中对
cell
进行存取的时候使用给定的类和标识符来操作
cell(
需要
cell
的时候
,
按照给定的模板来完成
)
//(绘制tableView的cell,这个@"aaa"和下面的@""一样)
cell:
selectionStyle : 设置cell被选中时样式
accessoryType : 设置辅助视图样式
indexPath.section == 0 && indexPath.row = 0 表示第0个分区第0行标题
//创建表视图
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.backgroundColor = [UIColor whiteColor];
//分割线的颜色
tableView.separatorColor = [UIColor redColor];
//分割线的样式(UITableViewCellSelectionStyleNone,或者将分割线的颜色调成透明色去掉分割线颜色)
// tableView.separatorStyle = UITableViewCellSelectionStyleNone;
//设置行高
tableView.rowHeight = 50;
//设置数据源
tableView.dataSource = self;
//设置代理
tableView.delegate = self;
//设置页脚(当创建一个空视图作为页脚的时候,会取消显示多余的cell)
tableView.tableFooterView = [UIView new];
//注册cell:如果tableView需要绘制cell,会根据注册时给定的类型还有对应的标识符来完成cell的绘制,并且在重用池中对cell进行存取的时候使用给定的类和标识符来操作cell(需要cell的时候,按照给定的模板来完成)
//(绘制tableView的cell,这个@"aaa"和下面的@""一样)
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:systemCellReuseIdentifier];//最好定义为静态的全局变量,不会出错
设置区头
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
// UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
// header.backgroundColor = [UIColor yellowColor];
//
// return header;
//
//}
//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
//设置区尾
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
label.text = @"You jump ! I can see you jumping";
label.backgroundColor = [UIColor blueColor];
return label;
}
//设置区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//设置区头标题(需要将区头注释)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//设置单独某一个区头标题
if(section == 4){
return @"D";
}
return @"Floating_SH";
}
//设置右侧索引(点击对应的索引,跳到对应的分区,数组的下标和分区的编号对应)
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *array = @[@"A",@"B",@"C",@"D",@"E",@"Z"];
return array;
}
//选中cell触发的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0){
FirstViewController *firstVC = [FirstViewController new];
[self.navigationController pushViewController:firstVC animated:YES];
}else if (indexPath.section == 0 && indexPath.row == 1){
SecondViewController *secondVC = [SecondViewController new];
[self.navigationController pushViewController:secondVC animated:YES];
}
NSLog(@"%ld--%ld",indexPath.section,indexPath.row);
}
#pragma mark -------------------
//设置分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 5;
}
//设置某一个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
//设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentifier"];//reuseIdentifier 重用标识符
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemCellReuseIdentifier];
cell.textLabel.text = @"王国维";
cell.detailTextLabel.text = @"以我观物,物物皆着我之色彩";
cell.imageView.image = [UIImage imageNamed:@"头像.jpg"];
//设置选中样式(UITableViewCellSelectionStyleNone取消样式)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置辅助视图样式
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//自定义辅助视图
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(0, 0, 51, 31)];
mySwitch.on = YES;
cell.accessoryView = mySwitch;
CGFloat red = arc4random() % 256 / 255.0;
CGFloat green = arc4random() % 256 / 255.0;
CGFloat blue = arc4random() % 256 / 255.0;
cell.contentView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
//让第0个分区的第0行标题改变
if(indexPath.section == 0 && indexPath.row == 0){
cell.textLabel.text = @"军哥";
}
//第0个分区第1行标题改变
NSIndexPath *myIndexPath = [NSIndexPath indexPathForItem:1 inSection:0];
if (myIndexPath == indexPath) {
cell.textLabel.text = @"小军哥";
}
return cell;
}
//创建表视图
UITableView *tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
tableView.backgroundColor = [UIColor whiteColor];
//分割线的颜色
tableView.separatorColor = [UIColor redColor];
//分割线的样式(UITableViewCellSelectionStyleNone,或者将分割线的颜色调成透明色去掉分割线颜色)
// tableView.separatorStyle = UITableViewCellSelectionStyleNone;
//设置行高
tableView.rowHeight = 50;
//设置数据源
tableView.dataSource = self;
//设置代理
tableView.delegate = self;
//设置页脚(当创建一个空视图作为页脚的时候,会取消显示多余的cell)
tableView.tableFooterView = [UIView new];
//注册cell:如果tableView需要绘制cell,会根据注册时给定的类型还有对应的标识符来完成cell的绘制,并且在重用池中对cell进行存取的时候使用给定的类和标识符来操作cell(需要cell的时候,按照给定的模板来完成)
//(绘制tableView的cell,这个@"aaa"和下面的@""一样)
[tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:systemCellReuseIdentifier];//最好定义为静态的全局变量,不会出错
设置区头
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//
// UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
// header.backgroundColor = [UIColor yellowColor];
//
// return header;
//
//}
//设置区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
//设置区尾
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
label.text = @"You jump ! I can see you jumping";
label.backgroundColor = [UIColor blueColor];
return label;
}
//设置区尾高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 30;
}
//设置区头标题(需要将区头注释)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//设置单独某一个区头标题
if(section == 4){
return @"D";
}
return @"Floating_SH";
}
//设置右侧索引(点击对应的索引,跳到对应的分区,数组的下标和分区的编号对应)
- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
NSArray *array = @[@"A",@"B",@"C",@"D",@"E",@"Z"];
return array;
}
//选中cell触发的事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
if(indexPath.section == 0 && indexPath.row == 0){
FirstViewController *firstVC = [FirstViewController new];
[self.navigationController pushViewController:firstVC animated:YES];
}else if (indexPath.section == 0 && indexPath.row == 1){
SecondViewController *secondVC = [SecondViewController new];
[self.navigationController pushViewController:secondVC animated:YES];
}
NSLog(@"%ld--%ld",indexPath.section,indexPath.row);
}
#pragma mark -------------------
//设置分区的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 5;
}
//设置某一个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 3;
}
//设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentifier"];//reuseIdentifier 重用标识符
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:systemCellReuseIdentifier];
cell.textLabel.text = @"王国维";
cell.detailTextLabel.text = @"以我观物,物物皆着我之色彩";
cell.imageView.image = [UIImage imageNamed:@"头像.jpg"];
//设置选中样式(UITableViewCellSelectionStyleNone取消样式)
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//设置辅助视图样式
//cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//自定义辅助视图
UISwitch *mySwitch = [[UISwitch alloc]initWithFrame:CGRectMake(0, 0, 51, 31)];
mySwitch.on = YES;
cell.accessoryView = mySwitch;
CGFloat red = arc4random() % 256 / 255.0;
CGFloat green = arc4random() % 256 / 255.0;
CGFloat blue = arc4random() % 256 / 255.0;
cell.contentView.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1];
//让第0个分区的第0行标题改变
if(indexPath.section == 0 && indexPath.row == 0){
cell.textLabel.text = @"军哥";
}
//第0个分区第1行标题改变
NSIndexPath *myIndexPath = [NSIndexPath indexPathForItem:1 inSection:0];
if (myIndexPath == indexPath) {
cell.textLabel.text = @"小军哥";
}
return cell;
}