tableview 高度适配内容(高度随着cell行数变化而动态变化)及(监听contentsize变化)


//评论布局

-( void )drawPingLunViewWithArr:( NSMutableArray *)arrData{

self . pinTab = [[ UITableView alloc ] initWithFrame : CGRectMake ( 0 , 0 , SCREEN_WIDTH , SCREEN_WIDTH ) style : UITableViewStylePlain ];

[ self . contentView addSubview : self . pinTab ];

[ self . pinTab mas_remakeConstraints :^( MASConstraintMaker *make) {

make. top . mas_equalTo ( self .szdz_view.mas_bottom). mas_offset ( TEXT_EDGE_DISTANCE );

make. left . right . mas_equalTo ( self .contentView);

make. height . mas_equalTo ( 200 *arrData.count+ 60 + 40 + 260 + 40 );

}];

[ self . pinTab layoutIfNeeded ];

self . pinTab . delegate = self ;

self . pinTab . dataSource = self ;

self . pinTab . scrollEnabled = YES ;

self . pinTab . bounces = NO ;

self . pinTab . estimatedRowHeight = 100 ;

self . pinTab . rowHeight = UITableViewAutomaticDimension ;

[ self . pinTab registerClass :[ UITableViewCell class ] forCellReuseIdentifier : @"cell" ];

self . pinTab . separatorStyle = UITableViewCellSeparatorStyleNone ;

//添加标题

self . pinTab . tableHeaderView = [[ UIView alloc ] init ];

[ self . pinTab registerClass :[ PingLunXQTabCell class ] forCellReuseIdentifier : @"PingLunXQTabCell" ];

[ self . pinTab registerClass :[ ShopXQXiangSiTabCell class ] forCellReuseIdentifier : @"ShopXQXiangSiTabCell" ];

//添加观察控件的contentSize变化

[ self . pinTab addObserver : self forKeyPath : @"contentSize" options : 0 context : NULL ];


}

//监听contentSize变化改变约束

- ( void )observeValueForKeyPath:( NSString *)keyPath ofObject:( id )object change:( NSDictionary *)change context:( void *)context {

CGRect frame = self . pinTab . frame ;

frame. size = self . pinTab . contentSize ;

[ self . pinTab mas_remakeConstraints :^( MASConstraintMaker *make) {

make. top . mas_equalTo ( self .szdz_view.mas_bottom). mas_offset ( TEXT_EDGE_DISTANCE );

make. left . right . mas_equalTo ( self .contentView);

make. height . mas_equalTo (frame.size.height);

}];

}

-( NSInteger )numberOfSectionsInTableView:( UITableView *)tableView{

return 2 ;

}

-( NSInteger )tableView:( UITableView *)tableView numberOfRowsInSection:( NSInteger )section{

if (section== 0 ) {

return 2 ;

} else {

return 1 ;

}

}

-( UITableViewCell *)tableView:( UITableView *)tableView cellForRowAtIndexPath:( NSIndexPath *)indexPath{

if (indexPath. section == 0 ) {

PingLunXQTabCell *cell = [tableView dequeueReusableCellWithIdentifier : @"PingLunXQTabCell" ];

cell. selectionStyle = UITableViewCellSelectionStyleNone ;

cell. vc = self ;

cell. arrImgv = [ @[ @"2" ] mutableCopy ]; //控制图片显隐性

return cell;

} else {

ShopXQXiangSiTabCell *cell = [tableView dequeueReusableCellWithIdentifier : @"ShopXQXiangSiTabCell" ];

cell. selectionStyle = UITableViewCellSelectionStyleNone ;

cell. vc = self ;

return cell;

}

}

-( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath{

[tableView deselectRowAtIndexPath :indexPath animated : NO ];

}

-( CGFloat )tableView:( UITableView *)tableView heightForRowAtIndexPath:( NSIndexPath *)indexPath{

return UITableViewAutomaticDimension ;

}


//头高度

-( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section{

return 60.f ;

}

//尾高度

-( CGFloat )tableView:( UITableView *)tableView heightForFooterInSection:( NSInteger )section{

return 30.f ;

}

//表头

-( UIView *)tableView:( UITableView *)tableView viewForHeaderInSection:( NSInteger )section{

UIView *hv = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , SCREEN_WIDTH , 60 )];

hv. backgroundColor = [ UIColor whiteColor ];

if (section== 0 ) {

self . pinLun_numL = [[ UILabel alloc ] init ];

[hv addSubview : self . pinLun_numL ];

self . pinLun_numL . font = MIDDLE_LIT_FONT ;

self . pinLun_numL . textColor = TEXT_HEX_MID_GRAY ;

[ self . pinLun_numL mas_makeConstraints :^( MASConstraintMaker *make) {

make. left . mas_equalTo (hv.mas_left). mas_offset ( TEXT_EDGE_DISTANCE );

make. centerY . mas_equalTo (hv.mas_centerY);

}];

self . pinLun_numL . text = [ NSString stringWithFormat : @"评论( %ld )" , 1 2 ];

UIImageView *pl_imgv = [[ UIImageView alloc ] init ];

pl_imgv. userInteractionEnabled = YES ;

[hv addSubview :pl_imgv];

pl_imgv. image = [ UIImage imageNamed : @"s_d_xiangyoujiantou" ];

[pl_imgv mas_makeConstraints :^( MASConstraintMaker *make) {

make. right . mas_equalTo (hv). mas_offset (- TEXT_EDGE_DISTANCE );

make. centerY . mas_equalTo (hv.mas_centerY);

make. size . mas_equalTo (CGSizeMake( 6 , 10 ));

}];

self . pingLun_fenL = [[ UILabel alloc ] init ];

[hv addSubview : self . pingLun_fenL ];

self . pingLun_fenL . font = MIDDLE_LIT_FONT ;

[ self . pingLun_fenL mas_makeConstraints :^( MASConstraintMaker *make) {

make. right . mas_equalTo (pl_imgv.mas_right). mas_offset (- TEXT_LINE_DISTANCE );

make. centerY . mas_equalTo (hv.mas_centerY);

}];

self . pingLun_fenL . attributedText = [ self getAttriStringWithPingLunString : @"99%" ];

UITapGestureRecognizer *tap = [[ UITapGestureRecognizer alloc ] initWithTarget : self action : @selector (clickedPingLunXiangQing:)];

[hv addGestureRecognizer :tap];

} else {

UILabel *lab_xs = [[ UILabel alloc ] init ];

[hv addSubview :lab_xs];

lab_xs. font = MIDDLE_LIT_FONT ;

lab_xs. textColor = TEXT_HEX_MID_GRAY ;

lab_xs. text = @"相似商品" ;

[lab_xs mas_makeConstraints :^( MASConstraintMaker *make) {

make. centerX . mas_equalTo (hv.mas_centerX). mas_offset ( TEXT_LINE_DISTANCE + 24 / 2 );

make. bottom . mas_equalTo (hv.mas_bottom);

}];

UIImageView *xs_imgv = [[ UIImageView alloc ] init ];

xs_imgv. userInteractionEnabled = YES ;

[hv addSubview :xs_imgv];

xs_imgv. image = [ UIImage imageNamed : @"s_d_xiangsi-biao" ];

[xs_imgv mas_makeConstraints :^( MASConstraintMaker *make) {

make. right . mas_equalTo (lab_xs.mas_left). mas_offset (- TEXT_LINE_DISTANCE );

make. centerY . mas_equalTo (lab_xs.mas_centerY);

make. size . mas_equalTo (CGSizeMake( 24 , 24 ));

}];


}

return hv;

}

//区尾

-( UIView *)tableView:( UITableView *)tableView viewForFooterInSection:( NSInteger )section{

UIView *view = [[ UIView alloc ] initWithFrame : CGRectMake ( 0 , 0 , SCREEN_WIDTH , 30 )];

view. backgroundColor = [ UIColor whiteColor ];

if (section== 0 ) {

UIButton *btn = [ UIButton buttonWithType : UIButtonTypeCustom ];

[view addSubview :btn];

btn. titleLabel . font = LITTLE_L_FONT ;

[btn setTitleColor : TEXT_HEX_LIGHT_L_GRAY forState : UIControlStateNormal ];

btn. layer . cornerRadius = 20 / 2 ;

btn. layer . borderColor = TEXT_HEX_LIGHT_L_GRAY . CGColor ;

btn. layer . borderWidth = 1 ;

[btn mas_makeConstraints :^( MASConstraintMaker *make) {

make. center . mas_equalTo (view);

make. size . mas_equalTo (CGSizeMake( 106 , 20 ));

}];

[btn setTitle : @"查看全部评论" forState: UIControlStateNormal ];

[btn addTarget : self action : @selector (clickedChaKanPingLun:) forControlEvents : UIControlEventTouchUpInside ];

} else {

UILabel *lab_xs = [[ UILabel alloc ] init ];

[view addSubview :lab_xs];

lab_xs. font = MIDDLE_LIT_FONT ;

lab_xs. textColor = TEXT_HEX_LIGHT_L_GRAY ;

lab_xs. text = @"上拉查看商品详情" ;

[lab_xs mas_makeConstraints :^( MASConstraintMaker *make) {

make. centerX . mas_equalTo (view.mas_centerX). mas_offset ( TEXT_LINE_DISTANCE + 16 / 2 );

make. centerY . mas_equalTo (view.mas_centerY);

}];

self . shanglaImgv = [[ UIImageView alloc ] init ];

self . shanglaImgv . userInteractionEnabled = YES ;

[view addSubview : self . shanglaImgv ];

self . shanglaImgv . image = [ UIImage imageNamed : @"s_d_xiangshang-biao" ];

[ self . shanglaImgv mas_makeConstraints :^( MASConstraintMaker *make) {

make. right . mas_equalTo (lab_xs.mas_left). mas_offset (- TEXT_LINE_DISTANCE );

make. centerY . mas_equalTo (view.mas_centerY);

make. size . mas_equalTo (CGSizeMake( 24 , 24 ));

}];

}

return view;

}



版权声明:本文为zhanglizhi111原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。