https://blog.csdn.net/q562679764/article/details/48553647
tableview分组样式每组section之间距离的调整
https://www.jianshu.com/p/6206d2c8efe0
iOS 固定UITableView的cell.imageView.image图片大小
https://www.jianshu.com/p/1d9b6e95f0f3
//
// ViewController.m
// UITableView-表视图控件
//
// Created by Liu,Wenbo(TBRD) on 2020/5/29.
// Copyright © 2020 Liu,Wenbo(TBRD). All rights reserved.
//
#import "ViewController.h"
//符合UITableViewDelegate,UITableViewDataSource协议
@interface ViewController () <UITableViewDelegate,UITableViewDataSource>
{
NSArray *_dataArray;
UITableView *tableView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化数据源数组
_dataArray = @[@"1",@"2",@"3"];
//创建一个tableView
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) style:UITableViewStyleGrouped];
/**样式
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view 默认平铺的风格
UITableViewStyleGrouped, // sections are grouped together 分组的风格
UITableViewStyleInsetGrouped API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(tvos) // grouped sections are inset with rounded corners
};
*/
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
[btn setTitle:@"编辑" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
//设置分割线内容部分的位置
//viewTable.separatorInset = UIEdgeInsetsMake(0, 40, 0, 0);
//设置代理和数据源
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
}
//action方法 通过按钮的方法 改变tableview的编辑模式
-(void)action:(UIButton *)btn{
if(tableView.isEditing){
tableView.editing = NO;
[btn setTitle:@"编辑" forState:UIControlStateNormal];
}
else{
tableView.editing = YES;
[btn setTitle:@"完成" forState:UIControlStateNormal];
}
}
#pragma mark - 实现代理方法
//设置分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 10;
}
//设置每个分区的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
switch (section) {
case 0:
return 1;
break;
case 1:
return 2;
break;
default:
return 1;
}
}
// 设置每个行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0){//第一个分区
return 60;
}
else if(indexPath.section == 1){//第二个分区
if(indexPath.row == 0){ //第二个分区的第一行
return 40;
}
else{
return 30;
}
}
return 44;
}
//对tableViewCell中的 行 进行设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCell *cell = [[UITableViewCell alloc] init];
// cell.textLabel.text = _dataArray[indexPath.section * 1 + indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
return cell;
}
//设置tableView的头视图
//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
// view.backgroundColor = [UIColor blueColor];
// return view;
//}
//设置tableView的尾视图
//-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 0)];
// view.backgroundColor = [UIColor redColor];
// return view;
//}
//设置头视图的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
// return 30;
//}
//设置尾视图的高度
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
// return 30;
//}
//设置tableView被编辑时的状态风格,如果不设置,默认都是删除风格
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.section == 0){
//在第一分支 是插入风格
return UITableViewCellEditingStyleInsert;
}
else{
// 在第二分支 是删除风格
return UITableViewCellEditingStyleDelete;
}
}
//tableView接受编辑时调用的方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
/**commitEditingStyle 编辑风格
typedef NS_ENUM(NSInteger, UITableViewCellEditingStyle) {
UITableViewCellEditingStyleNone,没有编辑操作
UITableViewCellEditingStyleDelete,删除 (默认)
UITableViewCellEditingStyleInsert 插入操作
};
*/
NSLog(@"在这里处理数据,section = %li,row = %li",indexPath.section,indexPath.row);
}
//设置每个分区头部的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [NSString stringWithFormat:@"%li",section + 1 ];
}
//tableView索引栏相关代理方法
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
}
//设置索引栏标题对应的分区
-(NSInteger )tableView:(UITableView *)tableView sectionForSectionIndexTitle:(nonnull NSString *)title atIndex:(NSInteger)index{
NSLog(@"选中%ld分区",index + 1);
//NSLog(@"%li",tableView.numberOfSections);//有多少分区
return tableView.numberOfSections - 1;
}
@end