estimatedRowHeight
(属性或代理方法),会去获取全部数据源的cell高度(包括屏幕外,这可能涉及没必要要的计算)estimatedRowHeight
后,只获取部分cell高度。- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndex:indexPath]; //会引起死循环,见第一张图
}
复制代码
initWithStyle: reuseIdentifier:
designated初始化方法若是cell高度和webview有关,须要在加载完更新高度的话web
如:新闻详情页下的推荐文件。 最简单的作法,在Header中放webView,tableView会本身处理Header的高度
。 webView加载完成后,修改webView的frame,而后刷新tableview的高度。objective-c
另外一种作法较繁琐,体验也很差。 思路值得借鉴,经过设置ContentInset,让WebView做为subView来处理。缓存
//webview
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
// 一开始是webview在滚动,tablview并无滚动。
// 直到webivew到底,其scrollview滑不动了。
// 此时,tableview的scrollview才接管滑动(tableview在webview下面,因此后接收手势)
// 此时若是下滑,由于禁止了webview的scrollview滑动,因此滑动的是tablview的scrollview。
if(self.tableView.contentOffset.y==0) {
[self.webView.scrollView setScrollEnabled:YES];
}else {
[self.webView.scrollView setScrollEnabled:NO];
}
}
//tableview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if(self.tableView.contentOffset.y==0) {
[self.webView.scrollView setScrollEnabled:YES];
}else {
[self.webView.scrollView setScrollEnabled:NO];
}
}
复制代码
单例键盘对象
,2相似于MVVM,cell持有本身的数据源、tableview
。UITableViewStyle
。
//delegate for headerInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
//返回的view的frame,决定不了真实高度。
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//高度由这决定
}
//style是readonly属性,建立时决定。
//若是是tableviewcontroller,则须要使用xib文件修改。
复制代码
场景:复杂Cell,层级较深,须要利用VC的资源时。 Cell的内部响应托管给VC。如:头条删除新闻要求选缘由的弹框。bash
cell的方法
。如:微博评论展开、微信增长评论等微信
修改tableview的bounds。app
微信聊天窗口,点文本框,对话栏不论contentOffset都滚动到底部 主要是contentSize和bounds。oop
- (void)tableViewScrollsToBottom {
if(self.chatDataArray.count<1){
return;
}
//若是不到一屏,就不拖到最下
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.chatDataArray.count-1 inSection:0]];
CGFloat lastCellMaxY = CGRectGetMaxY(cell.frame);
if(cell!=nil && lastCellMaxY<self.tableView.frame.size.height) {
return;
}
CGFloat yOffset = 0;
//若是tableview的contentSize超过table的bounds高度,就向上滚动
if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
yOffset = self.tableView.contentSize.height - self.tableView.bounds.size.height;
}
[self.tableView setContentOffset:CGPointMake(0, yOffset) animated:NO];
// SHWCRLog([NSString stringWithFormat:@"after tableView.contentSize %f, contentOffset %f", self.tableView.contentSize.height, self.tableView.contentOffset.y]);
}
复制代码
//TableView
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
AModel *model = self.datas[indexPath.row];
[cell start_show:model.uiModel];
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath {
[cell stop_show];
}
//Cell
- (void)start_show:(AModel*)model {
NSTimer *timer = [NSTimer timerWithTimeInterval:3
target:self
selector:@selector(firedTimer:)
userInfo:model repeats:NO];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
//避免cell复用的问题,使用队列来管理timer。
@synchronized(self.timerArray) {
[self.timerArray addObject:timer];
}
}
- (void)stop_show {
@synchronized(self.timerArray) {
if(self.timerArray.firstObject) {
NSTimer *timer = self.timerArray.firstObject;
[timer invalidate];
[self.timerArray removeObjectAtIndex:0];
}
}
}
- (void)firedTimer:(NSTimer *)timer {
@synchronized(self.timerArray) {
[self.timerArray removeObject:timer];
}
... //埋点
}
复制代码
网上查了下,说是由于iOS11中对自动高度的默认值改变形成的。能够经过关闭高度自动设置修复。 iOS10以后,能够用自带的refreshControl,iOS开发之UIRefreshControl使用踩坑布局
Tableview是一个ScrollView,须要知道contentSize,才能有合理ScrollView的预估。 那么,Tableview是如何估计contentSize的呢?性能
tableView.rowHeight = 88;
tableView.rowHeight = UITableViewAutomaticDimension;
复制代码
此时,contentSize= cell高度*数据源个数 + header高度等ui
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return xxx
}
复制代码
在没有设置estimatedRowHeight时,就会询问每一个cell的高度。
在cell的layoutSubviews
布局完成后,拿到cell的高度(Masonry的话,要加一句[self layoutIfNeeded]
)。
_tableView.bounces = NO;
_tableView.estimatedRowHeight = 152;
_tableView.rowHeight = UITableViewAutomaticDimension;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
复制代码
[self.tableView beginUpdates];
[self.tableView endUpdates];
复制代码
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
复制代码
全局刷新: [self.tableView reloadData];
修改局部刷新, reloadRowsAtIndexPaths
删除局部刷新,deleteRowsAtIndexPaths
- (NSArray*)visibleCells;
- (NSArray*)indexPathsForVisibleRows;
复制代码
//复杂Cell,根据需求看是否持有VC、TableView、VM
@protocol SomeCellDelegate
- (void)onDelClicked:(id)sender; //cell的内部响应托管给VC。如:头条删除新闻要求选缘由的弹框。
@end
@property (nonatomic, strong)SomeModel *model;
@property (nonatomic, weak)UIViewController<SomeCellDelegate> * cellDelegate;
@property (nonatomic, weak)UITableView * tableView; //如:cell高度变化,要求TableView刷新高度
//TableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
[self setUp];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self) {
[self setUp];
}
return self;
}
- (void)setUp {
self.backgroundColor = [UIColor colorWithHexString:[SHWRUMetaHolder inst].feed.bgColor alpha:1];
[self.contentView addSubview:self.titleLabel];
[self.contentView addSubview:self.contentImageView];
[self.contentView addSubview:self.separateLine];
}
- (void)setModel:(SHWRUNewsWrappedModel *)model {
_model = model;
[self updateContent];
[self updateLayout];
}
- (void)updateLayout {
}
- (void)updateContent {
}
复制代码