方式一:解偶的方式創(chuàng)建TableView
#import "TableViewController.h"
#import "TestTableView.h"
@interface TableViewController ()
@property(nonatomic , strong) TestTableView * tableView;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
[self loadDataisFirst];
}
#pragma mark - 交互
- (void)detailClick:(NSIndexPath *)indexPath
{
NSLog(@"%ld-----------%ld",(long)indexPath.section,(long)indexPath.row);
}
#pragma mark - 網(wǎng)絡(luò)
- (void)loadDataisFirst
{
self.tableView.array = @[@"-1-",@"-2-",@"3-3",@"5-5",@"6-6"];
}
#pragma mark - 懶加載
-(UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[TestTableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
_tableView.backgroundColor = UIColor.grayColor;
[self.view addSubview:_tableView];
_tableView.itemClick = ^(NSIndexPath * _Nonnull indexPath) {
[self detailClick:indexPath];
};
}
return _tableView;
}
TestTableView
@interface TestTableView : UITableView
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, copy) void (^itemClick)(NSIndexPath *indexPath);
@end
#import "TestTableView.h"
#import "TestTableViewCell.h"
#import "TestTableViewHeaderFooterView.h"
@interface TestTableView () <UITableViewDelegate, UITableViewDataSource>
@end
@implementation TestTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
self = [super initWithFrame:frame style:style];
if (self) {
self.separatorStyle = UITableViewCellSeparatorStyleNone;
self.autoresizingMask = UIViewAutoresizingFlexibleHeight;
//
self.delegate = self;
self.dataSource = self;
[self registerClass:[TestTableViewCell class] forCellReuseIdentifier:NSStringFromClass(TestTableViewCell.class)];
[self registerClass:[TestTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(TestTableViewHeaderFooterView.class)];
}
return self;
}
#pragma mark - 交互
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.array.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(TestTableViewCell.class) forIndexPath:indexPath];
cell.reasonLabel.text = [NSString stringWithFormat:@"我是第%ld個(gè)cell",(long)indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if (self.itemClick) {
self.itemClick(indexPath);
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
TestTableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(TestTableViewHeaderFooterView.class)];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = UIColor.blueColor;
return footerView;
}
#pragma mark - setter
- (void)setArray:(NSArray *)array
{
_array = array;
[self reloadData];
}
方式二:保守的創(chuàng)建方式TableView
estimated:預(yù)估
//高度自適應(yīng)
self.tableView.estimatedRowHeight = 88;
self.tableView.rowHeight = UITableViewAutomaticDimension;
//cell的細(xì)節(jié)
[self.contentView addSubview:self.retryBtn];
//在iOS 11上運(yùn)行tableView向下偏移64px或者20px西雀,因?yàn)閕OS 11廢棄了automaticallyAdjustsScrollViewInsets,
//而是給UIScrollView增加了contentInsetAdjustmentBehavior屬性。避免這個(gè)坑的方法是要判斷
if (@available(iOS 11.0, *)) {
_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}else {
self.automaticallyAdjustsScrollViewInsets = NO;
}
//tableView的sectionHeader、sectionFooter高度與設(shè)置不符觅廓,因?yàn)閠ableView的estimatedRowHeight觉啊、
//estimatedSectionHeaderHeight、 estimatedSectionFooterHeight三個(gè)高度估算屬性由默認(rèn)的0變成了
//UITableViewAutomaticDimension零聚。最簡單的方法就是直接設(shè)置為0榨了。
_tableView.estimatedSectionHeaderHeight = 0;
_tableView.estimatedSectionFooterHeight = 0;
_tableView.estimatedRowHeight = 0;
TableViewController.h
#import "TableViewController.h"
#import "TestTableViewCell.h"
#import "TestTableViewHeaderFooterView.h"
@interface TableViewController ()<UITableViewDelegate , UITableViewDataSource>
@property(nonatomic , strong) UITableView * tableView;
@end
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
[self.view addSubview:self.tableView];
}
#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(TestTableViewCell.class) forIndexPath:indexPath];
cell.reasonLabel.text = [NSString stringWithFormat:@"我是第%ld個(gè)cell",(long)indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
TestTableViewHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(TestTableViewHeaderFooterView.class)];
return headerView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] init];
footerView.backgroundColor = UIColor.blueColor;
return footerView;
}
#pragma mark - 懶加載
-(UITableView *)tableView
{
if (_tableView == nil) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
// _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[TestTableViewCell class] forCellReuseIdentifier:NSStringFromClass(TestTableViewCell.class)];
[_tableView registerClass:[TestTableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass(TestTableViewHeaderFooterView.class)];
_tableView.backgroundColor = UIColor.grayColor;
[self.view addSubview:_tableView];
}
return _tableView;
}
@end
TestTableViewCell
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestTableViewCell : UITableViewCell
@property(nonatomic,strong)UILabel *reasonLabel;
@end
NS_ASSUME_NONNULL_END
#import "TestTableViewCell.h"
@implementation TestTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// self.selectionStyle = UITableViewCellSelectionStyleNone;
[self setUI];
[self layout];
}
return self;
}
- (void)setUI{
NSLog(@"---");
[self.contentView addSubview:self.reasonLabel];
}
- (void)layout{
self.reasonLabel.frame = CGRectMake(0, 0, 300, 100);
}
#pragma mark - 懶加載
- (UILabel *)reasonLabel{
if (!_reasonLabel) {
_reasonLabel = [[UILabel alloc]init];
_reasonLabel.text = @"cell";
}
return _reasonLabel;
}
@end
TestTableViewHeaderFooterView
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface TestTableViewHeaderFooterView : UITableViewHeaderFooterView
@property(nonatomic,strong)UILabel *reasonLabel;
@end
NS_ASSUME_NONNULL_END
#import "TestTableViewHeaderFooterView.h"
@implementation TestTableViewHeaderFooterView
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithReuseIdentifier:reuseIdentifier];
if (self) {
[self setUI];
[self layout];
self.contentView.backgroundColor = UIColor.redColor;
}
return self;
}
- (void)setUI{
NSLog(@"+++");
[self.contentView addSubview:self.reasonLabel];
}
- (void)layout{
self.reasonLabel.frame = CGRectMake(0, 0, 200, 30);
}
#pragma mark - 懶加載
- (UILabel *)reasonLabel{
if (!_reasonLabel) {
_reasonLabel = [[UILabel alloc]init];
_reasonLabel.text = @"TableViewHeaderFooterView";
}
return _reasonLabel;
}
@end
用xib加約束和用masonry加代碼約束都是的的cell自適應(yīng)高度的使用方法
加好約束后煎谍,然后告訴tableView自己去適應(yīng)高度就可以了。有兩種寫法:
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 100;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
這個(gè)的意思就是告訴tableView龙屉,你需要自己適應(yīng)高度呐粘,我不給你算啦哈哈哈。但是我們需要告訴它一個(gè)大概高度转捕,例如上面的100作岖,理論上這個(gè)是可以隨便寫的,并不影響顯示結(jié)果瓜富,但是越接近真實(shí)高度越好鳍咱。
其實(shí)section的header和footer也是可以自動(dòng)適應(yīng)的降盹,對應(yīng)的方法有
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForFooterInSection:(NSInteger)section;
點(diǎn)擊狀態(tài)欄就有幾率不能精確滾動(dòng)到頂部了(不太算bug的bug)
如果我們用了自動(dòng)計(jì)算高度的方法与柑,又調(diào)用了tableView的reloadData方法(例如我們的數(shù)據(jù)有分頁的時(shí)候谤辜,加載完下一頁的數(shù)據(jù)后會(huì)去刷新tableView)。這時(shí)候就會(huì)出現(xiàn)問題价捧,點(diǎn)擊狀態(tài)欄就有幾率不能精確滾動(dòng)到頂部了丑念。
@property (nonatomic, strong) NSMutableDictionary *heightAtIndexPath;//緩存高度所用字典
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *height = [self.heightAtIndexPath objectForKey:indexPath];
if(height)
{
return height.floatValue;
}
else
{
return 100;
}
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSNumber *height = @(cell.frame.size.height);
[self.heightAtIndexPath setObject:height forKey:indexPath];
}
解釋一下,就是用一個(gè)字典做容器结蟋,在cell將要顯示的時(shí)候在字典中保存這行cell的高度脯倚。然后在調(diào)用estimatedHeightForRowAtIndexPath方法時(shí),先去字典查看有沒有緩存高度嵌屎,有就返回推正,沒有就返回一個(gè)大概高度。
常規(guī)的cell緩存高度
因?yàn)楫?dāng)tableView滾動(dòng)時(shí)會(huì)不停的回調(diào)- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;這個(gè)代理方法宝惰,當(dāng)cell的高度需自適應(yīng)內(nèi)容時(shí)植榕,就意味著每次回調(diào)這個(gè)方法時(shí)都要計(jì)算高度,而計(jì)算是要花時(shí)間了尼夺,在用戶體驗(yàn)上的體現(xiàn)就是卡頓尊残。為了避免重復(fù)且無意義的計(jì)算cell高度,緩存高度就顯得尤為重要了淤堵。
/** 緩存cell高度的數(shù)組 */
@property (nonatomic,strong) NSMutableArray *heightArray;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
CGFloat height;
if (self.heightArray.count > indexPath.row) {
// 如果有緩存的高度寝衫,取出緩存高度
height = [self.heightArray[indexPath.row] floatValue];;
}else{
// 無緩存高度,計(jì)算高度拐邪,并加入數(shù)組
// 高度根據(jù)評論內(nèi)容多少自適應(yīng)
CQGoodsCommentModel *model = self.dataArray[indexPath.row];
// 列寬
CGFloat contentWidth = screenWidth-20;
// 用何種字體進(jìn)行顯示
UIFont *font = [UIFont systemFontOfSize:13];
// 計(jì)算size
CGSize size = [model.comment_content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
// 這裏返回需要的高度
height = size.height+60;
// 加入數(shù)組
[self.heightArray addObject:[NSNumber numberWithDouble:height]];
}
return height;
}
下拉刷新tableView時(shí)記得清空高度緩存數(shù)組慰毅。
最好的方案:放在model里,拿到數(shù)據(jù)的時(shí)候就提前計(jì)算了
tableView數(shù)據(jù)處理方案
- (void)loadData
{
self.searchPage = 1;
[self loadDataGoods:self.searchPage first:YES];
}
- (void)loadDataRefresh
{
self.searchPage = 1;
[self loadDataGoods:self.searchPage first:NO];
}
- (void)loadDataMore
{
self.searchPage++;
[self loadDataGoods:self.searchPage first:NO];
}
//成功
if (listArray.count < kPageSize) {
dispatch_async(dispatch_get_main_queue(), ^(){
[weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
});
}
if (weakSelf.searchPage == 1)
{
[weakSelf.modeArray removeAllObjects];
(listArray.count == 0) ? (weakSelf.notDataView.hidden = NO) : (weakSelf.notDataView.hidden = YES);
}
else if (weakSelf.searchPage > 1 && listArray.count == 0)
{
weakSelf.searchPage--;
}
//失敗
weakSelf.searchPage--;
if (weakSelf.modeArray.count > 0) {
//處理:吐司
return;
}
//處理:第一次加載扎阶,網(wǎng)絡(luò)不給力(重試)事富,加載失敗(重試)乘陪,吐司统台,(mj_footer,mj_header啡邑,hidden = YES)