Fork me on GitHub

UITableView常见操作

方法一:设置section中title(颜色)以及所在view的背景色
1
2
3
4
5
6
7
8
9
10
11
12
#1,设置section中title(颜色)以及所在view的背景色
-(void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.textColor = [UIColor redColor];
header.contentView.backgroundColor = [UIColor yellowColor];
}
方法二:索引的颜色以及背景色
1
2
3
4
5
6
更改索引的背景颜色:
self.tableView.sectionIndexBackgroundColor = [UIColor clearColor];
更改索引的文字颜色:
self.tableView.sectionIndexColor = [UIColor blueColor];
方法三:分割线到头
1
2
3
4
//分割线
// [cell setSeparatorInset:UIEdgeInsetsZero];
// [cell setPreservesSuperviewLayoutMargins:NO];
// [cell setLayoutMargins:UIEdgeInsetsZero];
方法四:侧滑删除
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma mark - TableView代理方法
/**
* 只要实现这个方法,左划cell出现删除按钮的功能就有了
* 用户提交了添加(点击了添加按钮)\删除(点击了删除按钮)操作时会调用
*/
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) { // 点击了“删除”
// 删除模型
[self.deals removeObjectAtIndex:indexPath.row];
// 刷新表格
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
//开启编辑模式指定返回编辑类型为UITableViewCellEditingStyleInsert,点击+会走这里
//else if (editingStyle == UITableViewCellEditingStyleInsert) { // 点击了+
//NSLog(@"+++++ %zd", indexPath.row);
}
}
//该方法返回的是你左滑时候出现的文字提示。
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
return @"删除";
}
tableview系统侧滑删除按钮修改
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
81,tableview系统侧滑删除按钮修改
//在自定义的cell中,重写该方法。
- (void)layoutSubviews{
[super layoutSubviews];
for (UIView *subView in self.subviews) {
if([subView isKindOfClass:NSClassFromString(@"UITableViewCellDeleteConfirmationView")]) {
// 拿到subView之后再获取子控件
UIView *deleteConfirmationView = subView.subviews[0];
for (UIView *deleteView in deleteConfirmationView.subviews) {
UIImageView *deleteImage = [[UIImageView alloc] init];
deleteImage.contentMode = UIViewContentModeScaleAspectFit;
//Delete为图片
deleteImage.image = [UIImage imageNamed:@"Delete"];
deleteImage.frame = CGRectMake(0, 0, deleteView.frame.size.width, deleteView.frame.size.height);
[deleteView addSubview:deleteImage];
}
}
}
}
编辑模式(新增或者删除)

进入编辑模式的点方法为:self.tableView.editing = YES;
注意:另一种set方法为:[self.tableView setEditing:!self.tableView.isEditing animated:YES];
区别:set方法可以设置是否添加动画效果,体验更好。

1
2
3
4
5
6
7
8
9
#点击左侧的+或-,会走上面的commitEditingStyle方法。
/**
* 这个方法决定了编辑模式时,每一行的编辑类型:insert(+按钮)、delete(-按钮)
*/
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
//偶数行:增加 奇数行:删除
return indexPath.row % 2 == 0? UITableViewCellEditingStyleInsert : UITableViewCellEditingStyleDelete;
}
方法五:cell的选中样式

image.png

方法六:cell自适应高度之:cell调用方法的顺序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1,先调用多次这个方法。-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
2,- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
#按照1、2的顺序,我们就在heightForRowAtIndexPath中拿不到Cell中计算的高度了。
#解决方案:
//前提:
#要使用iOS6.0中的dequeueReusableCellWithIdentifier:方法,不注册的话需要判断cell是否为空。
使用dequeueReusableCellWithIdentifier:
forIndexPath:方法,一定会返回单元格,不用判断cell是否为空。如果不存在,会自动帮我们创建。但是呢,有个弊端,使用这个方法加上下面的预估高度方法,还是会先走heightForRowAtIndexPath方法然后走cellForRowAtIndexPath方法。
//步骤
#1,加上个预估高度,使用estimatedRowHeight属性设置(推荐)或者使用代理方法estimatedHeightForRowAtIndexPath。。
#2,加上预估高度方法,这样系统会先走这个方法,系统知道有个大概高度,就会先走cellForRowAtIndexPath方法,最后再走heightForRowAtIndexPath方法返回准确的高度。

方法七:为什么需要强制布局然后再获取最下面一个控件的高度?
1
2
3
4
5
6
7
8
9
10
虽然先走的cellForRowAtIndexPath方法,然后再走heightForRowAtIndexPath。
但是呢,cellForRowAtIndexPath方法中的cell内容可能还没有加载完数据,
这时就已经走heightForRowAtIndexPath方法,导致高度不正确。
#解决方案:
#在cellforrow中model的set方法中,cell强制布局一下,
#然后再获取cell最下面一个控件的高度,然后记录到model中
#[cell layoutIfNeeded];//强制布局
#status.cellHeight = CGRectGetMaxY(self.jq_contentlbl.frame);//获取并记录高度
#注意:强制布局是UI界面上的布局所以不会耗性能。
方法八:tableview的stype设置为组类型的时候,section的头部和尾部会有一定的高度。去掉高度的方式为:
1
2
heightForHeaderInSection、heightForFooterInSection、heightForRowAtIndexPath中不能返回0或者0.0
#至少要有值,比如:0.01
方法九:不要在设置头部view或者尾部view,还有section的头部和尾部view中,直接创建view。
1
2
3
4
5
#设置tableview的头部和尾部:应该使用懒加载的方式展示头部或者尾部view,
不然,每次滚动的时候都会重新创建,这样就会卡顿,耗性能。
#设置section的头部和尾部view,应该每次都需要创建,
因为每个section都有头部和尾部view,内部会自动复用!
方法十:删除前两条数据

image.png

方法十一:调用reloadData刷新的时候,不能在子线程中调用,会没效果。只能在主线程中调用reloadData。

方法十二:

方法十三:设置cell底部的分割线为一张图片的时候,底部约束要设置为-1,而不应该设置为0,因为cell的底部默认是有一条高度为1的分割线存在的。

方法十四:关闭tableview的弹性
self.myTableView.bounces = false;

方法十五:使用storyboard注意点:
1
2
1,背景图是一张图片时候,图片不能太大,如果太大,而且使用了自动布局,那么cell的高度就会适应图片的大小,导致cell高度不准确。
2,storyboard中的tableview或者其他控件一定要拖拽,而且保证拖拽的线是对的,否则可能会自动识别到别的sb中,还不报错,我擦,无语。

方法十六:点击cell中的按钮,触发一些操作

1
2
注意:虽然点击cell中的按钮,但是还是会走cellForRowAtIndexPath方法,而且model也就是点击按钮所在的行的model。
所以,除非要用cell中控件,那么我们可以直接在按钮的点击事件中操作当前行的model即可。

方法十七:cell显示全部和展示部分

1
之前可能已经提到过,点击cell中的按钮,会走cellforrow方法,也就是当前点击按钮所在的cell,那么只需要写个block回调就可以了。

showBtnMethod:就是按钮的点击方法

cellForRowAtIndexPath:方法