UITableViewCell里面嵌套UICollectionView出现tableviewcell被遮挡的bug

项目中要实现UITableViewCell里面嵌套UICollectionView, 通过监听UICollectionView的contentSize 改变UITableViewCell的高度. 使用过程中出现UITableViewCell被遮挡显示不全下个分区商品区头题目遮挡上一个商品.,在把cell滑出屏幕后显示正常的bug问题


1.UITableViewCell

1)文件.h

//  HomeMidousQuTabCell.h
//  MiDouShu
//
//  Created by mac on 2018/11/22.
//  Copyright © 2018年 zlz. All rights reserved.
//

#import <UIKit/UIKit.h>
@class HomeMidouGoodsListMjModel; //米豆商品

NS_ASSUME_NONNULL_BEGIN

@interface HomeMidousQuTabCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, copy) void(^reloadBlock)(NSInteger type);
@property(nonatomic,weak) UIViewController *vc;

@property(nonatomic,strong) UIImageView *mainImgv;
@property(nonatomic,strong) UICollectionView *myColView;
@property(nonatomic,strong) UICollectionViewFlowLayout *flowLayout;

@property(nonatomic,strong) NSMutableArray *arr_midouGoods; //米豆商品列表
@property(nonatomic,strong) NSMutableArray *arr_midouBanners; //米豆gg

@end

2).文件.m 

#import "HomeMidousQuTabCell.h"
//#import "HomeGoodsQuColCell.h"  //样式:米豆,价格 是一行显示 弃用---->>>现在使用米豆价格2行显示
#import "HomeGoodsQuSecStytleColCell.h" //首页商品样式 >>>>> 米豆,现金 2行显示

#import "HomeMidouGoodsListMjModel.h" //米豆商品
#import "HomeBannerMJModel.h"

#import "HomeGoodsDetailViewController.h" //商品详情

@interface HomeMidousQuTabCell ()

@end

@implementation HomeMidousQuTabCell

-(void)setVc:(UIViewController *)vc{
    _vc = vc;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self drawView];
    }
    return self;
}

-(void)drawView{
    [self drawMainImgv];
    [self drawCollectionView];
}
-(void)drawMainImgv{
    CGFloat imgv_w = SCREEN_WIDTH;
    CGFloat imgv_h = imgv_w*(150/375.0);
    
    self.mainImgv = [[UIImageView alloc]init];
    [self addSubview:self.mainImgv];
    self.mainImgv.userInteractionEnabled = YES;
    self.mainImgv.contentMode = UIViewContentModeScaleAspectFit;
    [self.mainImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.mas_left);
        make.size.mas_equalTo(CGSizeMake(imgv_w, imgv_h));
    }];
    self.mainImgv.backgroundColor = [UIColor clearColor];
    self.mainImgv.backgroundColor = [UIColor yellowColor];
    
    UITapGestureRecognizer *tap_ltp = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(jumpMainImgv1:)];
    [self.mainImgv addGestureRecognizer:tap_ltp];
}
-(void)drawCollectionView{
    //
    CGFloat cellWidth = (SCREEN_WIDTH-2*2-5*2)/3.0;
    
    //样式:米豆,价格 是一行显示 弃用
    //    CGFloat cellHeight = cellWidth*(170/125.0)
    //首页商品样式 >>>>> 米豆,现金 2行显示
    CGFloat cellHeight = cellWidth*(190/125.0);
    
    self.flowLayout = [[UICollectionViewFlowLayout alloc]init];
    self.flowLayout.itemSize = CGSizeMake(cellWidth, cellHeight+10);
    //管上下缝隙 minimumLineSpacing
    self.flowLayout.minimumLineSpacing = 10;
    //管左右缝隙 minimumInteritemSpacing
    self.flowLayout.minimumInteritemSpacing = 5;
    self.flowLayout.sectionInset = UIEdgeInsetsMake(5, 2, 5, 2);
    self.flowLayout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.0001);
    self.flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.0001);
    self.myColView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH) collectionViewLayout:self.flowLayout];
    self.myColView.delegate = self;
    self.myColView.dataSource = self;
    self.myColView.backgroundColor = [UIColor colorWithHexString:@"#f0f2f5" alpha:1.0f];
    self.myColView.showsHorizontalScrollIndicator = NO;
    self.myColView.showsVerticalScrollIndicator = NO;
    [self addSubview:self.myColView];
    [self.myColView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainImgv.mas_bottom).mas_offset(5);
        make.left.right.bottom.mas_equalTo(self);
    }];
    [self.myColView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    //米豆,现金一行
    //    [self.myColView registerClass:[HomeGoodsQuColCell class] forCellWithReuseIdentifier:@"HomeGoodsQuColCell"];
    //米豆,现金 2行
    [self.myColView registerClass:[HomeGoodsQuSecStytleColCell class] forCellWithReuseIdentifier:@"HomeGoodsQuSecStytleColCell"];
    
    
    [self.myColView setNeedsLayout];
    
    [self.myColView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
    
    [self.myColView reloadData];
    
}

-(void)layoutSubviews{
    [super layoutSubviews];

}
//监听contentSize变化改变约束
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {    
    CGSize size_cv = self.myColView.contentSize;
    
    CGFloat imgv_w = SCREEN_WIDTH;
    CGFloat imgv_h = imgv_w*(150/375.0);
    [self.mainImgv mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top);
        make.left.mas_equalTo(self.mas_left);
        make.size.mas_equalTo(CGSizeMake(imgv_w, imgv_h));
    }];
    [self.myColView setNeedsLayout];
    [self.myColView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mainImgv.mas_bottom).mas_offset(5);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(size_cv.height);
//        make.bottom.mas_equalTo(self.mas_top).mas_offset(size_cv.height);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
    
    [self.myColView layoutIfNeeded];
    [self.contentView layoutIfNeeded];
    [self layoutIfNeeded];

}

#pragma mark -- collectionVeiw delegate --
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.arr_midouGoods.count;
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeGoodsQuSecStytleColCell *cell= [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeGoodsQuSecStytleColCell" forIndexPath:indexPath];
    
    cell.kind = GoodsType_Midou_kind;
    HomeMidouGoodsListMjModel *midou = self.arr_midouGoods[indexPath.row];
    cell.midou = midou;

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeGoodsDetailViewController *vc = [[HomeGoodsDetailViewController alloc]init];
//    vc.hidesBottomBarWhenPushed = YES;
//    vc.goods_id = good_id;
//    vc.tehui = tehui;
    HomeMidouGoodsListMjModel *midou = self.arr_midouGoods[indexPath.row];

    vc.midou = midou;

    UITabBarController *tabBarVc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *nvc = [tabBarVc selectedViewController];
    [nvc pushViewController:vc animated:YES];
    
}

//分类主图跳转
-(void)jumpMainImgv1:(UITapGestureRecognizer *)tap{
    
}
//米豆广告
-(void)setArr_midouBanners:(NSMutableArray *)arr_midouBanners{
    _arr_midouBanners = arr_midouBanners;
    
    if(arr_midouBanners.count>0){
        HomeBannerMJModel *bannerModel = arr_midouBanners.firstObject;
        if ([bannerModel.ad_code containsString:@"http"] && [bannerModel.ad_code containsString:@"://"]) {
            [self.mainImgv  sd_setImageWithURL:[NSURL URLWithString:bannerModel.ad_code] placeholderImage:[UIImage imageNamed:@"place_temp_180_180"]];
            
        }else{
            [self.mainImgv  sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGE_IP_1,bannerModel.ad_code]] placeholderImage:[UIImage imageNamed:@"place_temp_180_180"]];
            
        }
        
        [self.myColView reloadData];
    }
}

//米豆商品
-(void)setArr_midouGoods:(NSMutableArray *)arr_midouGoods{
    _arr_midouGoods = arr_midouGoods;
    
    [self.myColView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

-(void)dealloc{
    [self.myColView removeObserver:self forKeyPath:@"contentSize"];
}

@end

2.主视图里面UITableView delegate方法里面

   
   // -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  //方法里面

   if(indexPath.section == 1){

        //使用
        HomeMidousQuTabCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HomeMidousQuTabCell" forIndexPath:indexPath];
        cell.arr_midouBanners = [self.baseModel.bannermidou mutableCopy];;
        cell.arr_midouGoods = [self.baseModel.midou_goods_list mutableCopy];

       //解决问题的关键代码
        cell.frame = tableView.bounds;
        [cell layoutIfNeeded];
       //刷新里面的试图不然图片可能凌乱
        [cell.myColView reloadData];
        
        return cell;

    }

 注:关键代码要从新布局tableviewcell

     cell.frame = tableView.bounds;

     [cell layoutIfNeeded];

     [cell.myColView reloadData];

猜你喜欢

转载自blog.csdn.net/zhanglizhi111/article/details/86293196