UIKit - UITableView
UITableVIew Cell 的 ImageView 设置图片的时候会因为图片过大,显示异常。最好不要用,自己自定义的就行
隐藏多余分割线
self.tableView.tableFooterView = [UIView new];
Cell 自适应高度
如何设置 Cell 的固定高度 ?
Xib 中使用 TableView可设置 Cell 高度,Cell 用自定义的 Xib 无法设置 Cell 的高度。
以 Label为例,如何自适应高度 ?
Cell 中的 Label 添加四周约束,不设置固定高度,
numberofline = 0
代理方法实现 ( Xib 和 StoryBoard )
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
非代理方法实现 ( Xib )
self.tableView.estimatedRowHeight = 30;// estimate 大概
self.tableView.rowHeight = UITableViewAutomaticDimension;// Dimension 尺寸面积
Xib 注册方法
注册 Xib 、storyboard
[tableView registerNib:[UINib nibWithNibName:@"" bundle:[NSBundle mainBundle]]forCellReuseIdentifier:@""];
[tableView dequeueReusableCellWithIdentifier:@"" forIndexPath:[NSIndexPath new]];//6.0
[tableView dequeueReusableCellWithIdentifier:@""];//5.0
非注册 storyboard
[tableView dequeueReusableCellWithIdentifier:@""]; //非注册方法不用indexPath
if (cell == nil) {
cell = [[[NSBundle mainBundle]loadNibNamed:@"" owner:nil options:nil] firstObject];
}
静态 Cell
- 第一步 : 设置 tableView 中 content 为 Static Cells
- 第二步 : 点击左边 控件列表 的 Table View Section 设置行数
注意:只能在 Storyboard 使用且为 UITableViewController,若在此时有添加 .m 和 .h 文件,如果要重写 UITableViewDataSource 可以调用 super 实现; tableView:cellForRowAtIndexPath: 使用 super 获取不到 cell,要用 dequeueReusableCellWithIdentifier
另附 : 在 UIViewController 设置静态 cell 默认会报错,报错信息如下:
Static table views are only valid when embedded in UITableViewControllerinstancesStatic table views are only valid when embedded in UITableViewControllerinstances
Xib 设置 TableHeaderView
- 第一步 : 先拖一个新的 UIView 与 File's Owner(通常是 Controller )的 view 并列关系,注意是并列关系,不是 subView 关系。
- 第二步 : 把刚刚新的 UIView 的 Simulated Metrics 的 Size 设置成 Freeform 。
- 第三步 : 把该 UIView 拖入 UITableView ,然后该 UIView 会自动作为 UITableView 的 tableHeaderView 或 tableFooterView。
去掉 Tableview 中 section 的 header view 粘性
附 : 分区尾只需要修改 edge 的下即可
// 去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 40;
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} // 逐渐下滑, 大小逐渐变小
else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
}
if (scrollView.contentOffset.y >= scrollView.contentSize.height - SCREEN_HEIGHT - 50) {
scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
}
给 TableView 或者 CollectionView 的 Cell 添加简单动画
TableView
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *array = tableView.indexPathsForVisibleRows;
NSIndexPath *firstIndexPath = array[0];
//设置anchorPoint
cell.layer.anchorPoint = CGPointMake(0, 0.5);
//为了防止cell视图移动,重新把cell放回原来的位置
cell.layer.position = CGPointMake(0, cell.layer.position.y);
//设置cell 按照z轴旋转90度,注意是弧度
if (firstIndexPath.row < indexPath.row) {
cell.layer.transform = CATransform3DMakeRotation(M_PI_2, 0, 0, 1.0);
}else{
cell.layer.transform = CATransform3DMakeRotation(- M_PI_2, 0, 0, 1.0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:1 animations:^{
cell.layer.transform = CATransform3DIdentity;
cell.alpha = 1.0;
}];
}
CollectionView
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row % 2 != 0) {
cell.transform = CGAffineTransformTranslate(cell.transform, kScreenWidth/2, 0);
}else{
cell.transform = CGAffineTransformTranslate(cell.transform, -kScreenWidth/2, 0);
}
cell.alpha = 0.0;
[UIView animateWithDuration:0.7 animations:^{
cell.transform = CGAffineTransformIdentity;
cell.alpha = 1.0;
} completion:^(BOOL finished) {
}];
}
HeaderView AutoLayout
CGFloat height = [self.headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; // 获取高度
CGRect frame = self.headerView.frame;
frame.size.height = height;
self.headerView.frame = frame;
self.tableView.tableHeaderView = self.headerView;
MJRefresh Bug
使用 UITableViewAutomaticDimension 来进行 Cell 自适配高度时候,用 MJRefresh , Cell 上拉会无限刷新。
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
解决方法:直接赋值,不使用代理方法
新增一条自适应 Cell 并滚动到最后一行
// 使用自适应的高度 cell 必须使用延迟才不会有滚动跳动的问题。
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataArray.count - 1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
});
可以上拉,不可以下拉
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
scrollView.bounces = scrollView.contentOffset.y > 0;
}
iOS 11
TableView 往下偏移 20
if (@available(iOS 11.0, *)) {
self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
} else {
// Fallback on earlier versions
self.automaticallyAdjustsScrollViewInsets = NO;
}
heightForHeaderInSection 不调用
iOS 11 默认开启 Self-Sizing,关闭 Self-Sizing 即可
tableView.estimatedRowHeight = 0;
tableView.estimatedSectionHeaderHeight = 0;
tableView.estimatedSectionFooterHeight = 0;