//tableView初始化用此方法
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style;
self.tableV.editing = !self.tableV.editing;//是否允许编辑
self.tableV.allowsMultipleSelectionDuringEditing = !self.tableV.allowsMultipleSelectionDuringEditing;//是否允许多选
//给tableView设置背景View
self.tableV.backgroundView = [[UIImageView alloc]initWithImage:[self getImageFromMainImagesDirectoryWithImageName:@"nonews"]];
self.tableV.backgroundView.contentMode = UIViewContentModeCenter;
//设置tableView是否可以滚动
self.tableV.scrollEnabled = NO;
二、UITableView的代理方法
//几个区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//每个区内几个cell
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
//cell的预估高度
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44.0f;
}
//cell的实际高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//返回UITableViewAutomaticDimension时cell的高度是自适应高度
//需要与预估高度配合使用
return UITableViewAutomaticDimension;
}
//cell的具体设置
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString * ID = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:ID];
}
return cell;
}
//选中 在多选状态下选中也会调用
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (self.tableV.editing) {
[self.selectArr addObject:indexPath];
}
}
//反选 在多选状态下反选也会调用
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (!self.tableV.editing) {
[self.selectArr removeObject:indexPath];
}
}
//长按向左滑动cell
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"长按向左滑动");
return UITableViewCellEditingStyleDelete;
}
//点击删除或减号或加号时会调用这个方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"点击删除按钮");
[self.dataArr removeObjectAtIndex:indexPath.row];
[self.tableV reloadData];
}
//拖动cell完成的时候会调用
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
NSLog(@"源:%@ 目标%@ ",sourceIndexPath,destinationIndexPath);
#if 0
if( sourceIndexPath.row<destinationIndexPath.row){
[self.dataArr insertObject:self.dataArr[sourceIndexPath.row] atIndex:destinationIndexPath.row+1];
[self.dataArr removeObjectAtIndex:sourceIndexPath.row];
}
else{
[self.dataArr insertObject:self.dataArr[sourceIndexPath.row] atIndex:destinationIndexPath.row];
[self.dataArr removeObjectAtIndex:sourceIndexPath.row+1];
}
#elif 1
id x = self.dataArr[sourceIndexPath.row];
[self.dataArr removeObject:x];
[self.dataArr insertObject:x atIndex:destinationIndexPath.row];
//[self.dataArr exchangeObjectAtIndex:sourceIndexPath.row withObjectAtIndex:destinationIndexPath.row];
#endif
[self.tableV reloadData];
}
三、其它重要方法
1.cell的提前注册
//注册cell
[self.tabelView registerClass:[MYCell class] forCellReuseIdentifier:@"id"];
//注册cell 有xib文件的cell
[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"id"];
//创建cell 显示内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
#if 0
//1.设定重用标识符
static NSString *cellID = @"id";
//2.依据重用标识符 从重用池里面获取cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
//3.判断cell是否存在
if (!cell) {
//4.如果不存在 创建cell
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
//如果使用系统的UITableViewCell 在cell上添加子控件的时候 需要在这个判断里创建、添加
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 40, 100, 40)];
label.backgroundColor = [UIColor redColor];
//在cell上添加子控件 需要添加到cell得contentView上
[cell.contentView addSubview:label];
label.tag = 100;
}
//4.让cell显示数据
cell.textLabel.text = @"数据";
//cell上子控件显示数据一定要在判断外
UILabel *subLabel = (id)[cell viewWithTag:100];
subLabel.text = @"显示数据";
//5.返回cell
return cell;
#elif 0
static NSString *cellID = @"id";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell) {
//NSBundle 统一管理程序资源的类
cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] lastObject];
}
cell.titleLabel.text = @"标题";
cell.nameField.text = @"名字";
return cell;
#else
//由于提前注册了这个cell 需要使用下面这个方法
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id" forIndexPath:indexPath];
return cell;
#endif
}
2.cell的创建
#pragma mark--创建cell的时候调用
//cell的初始化用initWithStyle而不是initwithframe
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self setUpAllChildView];
}
return self;
}
3.获取固定的cell
//获取某个固定的cell
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
4.设置cell的分割线
cell.separatorInset= UIEdgeInsetsMake(20, 50, 20, 50);
//设置cell的颜色
self.tabelView.separatorColor = [UIColor red];
4.自动使用导航条
//是否自动适应导航条与标签条
self.automaticallyAdjustsScrollViewInsets = YES;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
self.tableView.contentOffset = CGPointMake(0, 0);
self.tableView.tableHeaderView = headerView;
self.tableView.tableFooterView = footerView;
6.刷新
//刷新某一行
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
//具体某些分区刷新重载
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:(view.tag - 100)] withRowAnimation:UITableViewRowAnimationFade];
//刷新所有
[self.tableView reloadData];
7.选中状态
//去掉选中的状态
[self deselectRowAtIndexPath:indexPath animated:YES];