[toc]
indexPath 返回nil
请检查是不是在如viewWillAppear的方法中执行了reload
self.contentView.sendSubviewToBack(shadowView) // 请确保 shadowView 和 tableView 加到的是同一个父视图
否则,后添加的视图会盖住前面的视图。如tableView添加到cell上,shadowView添加到cell.contentView上
一、cell的自适应高度
1、基础设置
1 2 3 4 5
| tableView.estimatedRowHeight = 60;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewAutomaticDimension; }
|
2、某行不自适应
1 2 3 4 5 6
| - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == CQDetailInfoTypeBase) { return 60; } return UITableViewAutomaticDimension; }
|
3、如果自适应的cell的高度不够
1
| make.height.greaterThanOrEqualTo(@60); // 为了自适应的时候,最小的cell高度不会小于指定值
|
https://blog.csdn.net/wait_foryou/article/details/78256905
1 2 3 4 5 6 7 8 9 10 11 12 13
| // UITableView的Style为Plain时, 当tableView上移顶端的tableHeaderView会跟着滑出窗口, 而headerInsectionView则会悬浮固定在窗口顶端不随着滑动继续上移. // UITableView的Style为Grouped时, 当tableView上移顶端的tableHeaderView会跟着滑出窗口, 而headerInsectionView则会随着滑动继续上移.
// UITableView的Style为Plain时禁止headerInsectionView固定在顶端: - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat sectionHeaderHeight = 30; 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); } }
|
自己的另一种方法
1 2 3 4 5 6 7 8 9 10 11 12 13
| // 还是Group
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0; }
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [UIView new]; //view.backgroundColor = [UIColor greenColor]; return view; }
|
三、tableView等reloadData真正刷新结束后执行下一步操作
reloadData不会等tableView更新结束后才返回,而是立即返回,然后计算表高度,执行滚动之类的代码。
很明显这里的原因是因为数据比较大,一个run loop周期没执行完,tableView滚动时,表的高度不对。
问题场景:
情况1:reloadData后,scrollToRowAtIndexPath未滚动到指定区域
情况2:reloadData后,setContentOffset未滚动到指定区域
解决方法(两种):
1、强制刷新
1 2 3
| [self.myTableView reloadData]; [self.myTableView layoutIfNeeded]; [self.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([self.myTableView numberOfRowsInSection:2]-1) inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
|
layoutIfNeeded会强制重绘并等待完成。
2、线程等待
1 2 3 4 5
| [self.myTableView reloadData]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.myTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:([weakSelf.myTableView numberOfRowsInSection:2]-1) inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:NO]; });
|
[tableView reloadData]在主队列执行,而dispatch_get_main_queue()会等待主队列空闲后才执行。