1.设置分割线的风格和颜色
tableV.
separatorStyle
=
UITableViewCellSeparatorStyleSingleLine
;
tableV.
separatorColor
= [
UIColor
greenColor
];
2.设置单元格高度 动态设置单元格高度可以在代理方法里设置
tableV.
rowHeight
=
50
;
3.设置头视图
tableV.
tableHeaderView
= imgV;
4.刷新数据 会重新调用dataSource方法
[tableV
reloadData
];
5.获取并修改某个单元格的数据 但是只能修改显示了的cell的值 否则返回的cell为nil
NSIndexPath*indexPath = [NSIndexPathindexPathForRow:3inSection:0];
UITableViewCell
*cell = [tableV
cellForRowAtIndexPath
:indexPath];
cell.textLabel.text=@"fuck";
6.返回当前显示的所有单元格
NSArray
*visibles = [tableV
visibleCells
];
7.返回当前显示的所有单元格的
indexPath
NSArray
*indexPaths = [tableV
indexPathsForVisibleRows
];
8.将某一个单元格滑动到顶部
[tableV
scrollToRowAtIndexPath
:indexPath
atScrollPosition
:
UITableViewScrollPositionTop
animated
:
YES
];
9.cell单元格里面封装了三个子视图imageView,textLabel,detailTextLabel 都是只读属性
cell.
textLabel
.
text
=
_fontNames
[indexPath.
row
];
cell.
imageView
.
image
= [
UIImage
imageNamed
:
_imageNames
[indexPath.
row
]];
cell.
detailTextLabel
.
text
=
@"
字体
"
;
10.cell设置背景视图
cell.
backgroundView
= [[
UIImageView
alloc
]
initWithImage
:[
UIImage
imageNamed
:
@"1.jpg"
]];
11.cell右边配置类型 可以选打钩之类的
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
12.cell右边配置类型的视图 也可以自定义视图
cell.
accessoryView
= [[
UIImageView
alloc
]
initWithImage
:[
UIImage
imageNamed
:
@"1.jpg"
]];
13.设置字和图片的间隙 水平缩进(这些都对针对于系统提供样式的修改 实际开发中一般都是自定义样式 所以根本用不上)
cell.
indentationLevel
=
3.0
;
14.设置缩进宽度
cell.
indentationWidth
=
20
;
15.设置字和图片的间隙
cell.
separatorInset
=
UIEdgeInsetsMake
(
0
,
0
,
0
,
0
);
16.这个代理方法如果返回nil 将无法选中单元格
- (
nullable
NSIndexPath
*)tableView:(
UITableView
*)tableView willSelectRowAtIndexPath:(
NSIndexPath
*)indexPath {
return indexPath;
return indexPath;
}
17.单元格的重用 (最普通的方式)
UITableViewCell
*cell = [tableView
dequeueReusableCellWithIdentifier
:
kReuseCellID
];
if(cell ==nil) {
//相同
的内容放在里面
cell = [[
UITableViewCell
alloc
]
initWithStyle
:
UITableViewCellStyleSubtitle
reuseIdentifier
:
kReuseCellID
];
}
单元格重用时导致图片文字重叠问题解决方案
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"classifyReuseID" forIndexPath:indexPath];
if (cell) {
// 判断cell.contentView如果有子视图则移除
while ([cell.contentView.subviews lastObject] != nil) {
[[cell.contentView.subviews lastObject] removeFromSuperview];
}
}
18.dataSource方法实现设置标题功能
- (
nullable
NSString
*)tableView:(
UITableView
*)tableView titleForHeaderInSection:(
NSInteger
)section
19.设置组标题底部的文字
- (
nullable
NSString
*)tableView:(
UITableView
*)tableView titleForFooterInSection:(
NSInteger
)section
20.设置头视图高度 在delegate中
- (
CGFloat
)tableView:(
UITableView
*)tableView heightForHeaderInSection:(
NSInteger
)section
21.headView 设置颜色 直接设置背景颜色无法成功 要设置headView上的contentView才有效果
headView.
contentView
.
backgroundColor
= [
UIColor
greenColor
];
headView.backgroundColor= [UIColorredColor];
22.头视图上添加子视图要添加到contentView上
头视图也要使用重(chong)用机制 要用UITableViewHeadFootView创建头视图
[tableHeadView.
contentView
addSubview
:titleButton];
23.根据contentView上的子视图取出上面的button
UIButton
*titleButton = [tableHeadView.
contentView
.
subviews
lastObject
];
24.这两个分别是设置全局的头视图和组头视图
self
.tbV.tableHeaderView
- (
UIView
*)tableView:(
UITableView
*)tableView viewForHeaderInSection:(
NSInteger
)section
25.单元格可以设置静态的单元格 有些不需要运行时改变单元格个数的可以这样设置
26.设置导航栏 标签栏透明时 tableView自动向下偏移
- (void)viewDidAppear:(
BOOL
)animated {
[super viewDidAppear :animated];
// NSLog(@"%@",self.view);
// NSLog(@"%@",NSStringFromUIEdgeInsets(self.tbV.contentInset));
// 导航栏 标签栏设置透明时 tableView 都会分别向下偏移 64 向上偏移49
[super viewDidAppear :animated];
// NSLog(@"%@",self.view);
// NSLog(@"%@",NSStringFromUIEdgeInsets(self.tbV.contentInset));
// 导航栏 标签栏设置透明时 tableView 都会分别向下偏移 64 向上偏移49
}
27.tableView设置分组样式时 上面的空隙是整个tableView的头视图 要在代理方法里设置成0.1才可以做到看不出效果