美文网首页
QHP_35UITableView嵌套CollectionVie

QHP_35UITableView嵌套CollectionVie

作者: 风信子的梦想成真 | 来源:发表于2016-07-09 19:28 被阅读203次

    原博客地址:http://www.cnblogs.com/qiuwenbin/p/5046691.html 如有侵权还请告知

    // 如果想在TableView 上放 CollectionView, 最好要写在TableViewCell里, 并且CollectionView的代理方法都要写在TableViewCell里, 如果在ViewController里写的话,会因为cell重用的问题出现崩溃, 并且注册CollectionView的cell的时候, 会出现很多问题.

    // HPJRRMTableViewCell.h

    #import@class HomePageModel;

    typedef void(^HPJRRMTableViewCellBlock)(HomePageModel *hpModel);

    @interface HPJRRMTableViewCell : UITableViewCell

    @property (nonatomic, strong) HomePageModel *jrrmModel;

    @property (nonatomic, copy) HPJRRMTableViewCellBlock block;

    @end

    // block的定义是为了实现点击CollectionView跳转到指定的界面

    // HPJRRMTableViewCell.m的实现内容

    #import "HPJRRMTableViewCell.h"

    #import "HPJRRMCollectionViewCell.h"

    #import "HomePageModel.h"

    @interface HPJRRMTableViewCell ( )

    @property (nonatomic, strong) UICollectionView *collectionView;

    @property (nonatomic, strong) NSMutableArray *jrrmArr;

    @end

    @implementation HPJRRMTableViewCell

    - (void)setJrrmModel:(HomePageModel *)jrrmModel {

    if (_jrrmModel != jrrmModel) {

    _jrrmModel = jrrmModel;

    }

    // 刷新数据

    [self.collectionView reloadData];

    }

    - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

    // 在TableViewCell中调用getCollectionView方法

    [self getCollectionView];

    }

    return self;

    }

    - (void)getCollectionView {

    //  创建layout

    UICollectionViewFlowLayout *flayout = [[UICollectionViewFlowLayout alloc] init];

    flayout.itemSize = CGSizeMake((self.contentView.frame.size.width - 20) / 2, 180);

    //    flayout.scrollDirection = UICollectionViewScrollDirectionVertical;

    flayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    flayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 5);

    // 创建CollectionView

    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 375, 180) collectionViewLayout:flayout];

    self.collectionView.delegate = self;

    self.collectionView.dataSource = self;

    self.collectionView.backgroundColor = [UIColor whiteColor];

    [self.contentView addSubview:self.collectionView];

    //    self.collectionView.showsHorizontalScrollIndicator = NO;

    [self.collectionView registerClass:[HPJRRMCollectionViewCell class] forCellWithReuseIdentifier:@"HPJRRMCollectionViewCell"];

    // 网络请请求

    [self getData];

    }

    #pragma mark - 网络请求

    - (void)getData {

    }

    #pragma mark - UICollectionView必须实现的方法

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 4;

    //    return self.jrrmArr.count;

    }

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    HPJRRMCollectionViewCell *jrrmCollectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HPJRRMCollectionViewCell" forIndexPath:indexPath];

    jrrmCollectionViewCell.backgroundColor = [UIColor redColor];

    //    jrrmCollectionViewCell.collectionModel = self.jrrmArr[indexPath.item];

    return jrrmCollectionViewCell;

    }

    #pragma mark - UICollectionView实现点击跳转的方法

    - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    HomePageModel *model = [[HomePageModel alloc] init];

    model = self.jrrmModel;

    if (_block) {

    _block(model);

    }

    NSLog(@"实现点击跳转的方法section: %ld, item: %ld", indexPath.section, indexPath.item);

    }

    #HPJRRMCollectionViewCell.h中的文件实现

    #import@class HomePageModel;

    @interface HPJRRMCollectionViewCell : UICollectionViewCell

    @property (nonatomic, strong) HomePageModel *collectionModel;

    @end

    #HPJRRMCollectionViewCell.m中的文件实现

    #import "HPJRRMCollectionViewCell.h"

    #import "UIImageView+WebCache.h"

    #import "HomePageModel.h"

    // 延展

    @interface HPJRRMCollectionViewCell ()

    @property (nonatomic, strong) UIImageView *imageView;

    @end

    @implementation HPJRRMCollectionViewCell

    /** 初始化 */

    - (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];

    if (self) {

    // 创建控件

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

    self.imageView.backgroundColor = [UIColor redColor];

    [self.contentView addSubview:self.imageView];

    }

    return self;

    }

    /**  只做布局 不能赋值*/

    - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {

    // 图片布局

    self.imageView.frame = CGRectMake(0, 0, layoutAttributes.size.width, 180);

    }

    // setter方法赋值

    -(void)setCollectionModel:(HomePageModel *)collectionModel {

    if (_collectionModel != collectionModel) {

    _collectionModel = collectionModel;

    }

    //    [self.imageView sd_setImageWithURL:[NSURL URLWithString:collectionModel.coverImg] placeholderImage:[UIImage imageNamed:@"CommodityBackgrond"]];

    }

    @end

    //   在Controller里面的代理写的方法

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

    HPJRRMTableViewCell *jrrmCell = [tableView dequeueReusableCellWithIdentifier:@"jrrmCell"];

    //    jrrmCell.jrrmModel = self.jrrmArr[indexPath.row];

    // jrrmCell.textLabel.text = @"111";

    jrrmCell.block = ^(HomePageModel *model){

    SearchViewController *sVC = [[SearchViewController alloc] init];

    [self.navigationController pushViewController:sVC animated:YES];

    };

    jrrmCell.backgroundColor = [UIColor cyanColor];

    return jrrmCell;

    }

    相关文章

      网友评论

          本文标题:QHP_35UITableView嵌套CollectionVie

          本文链接:https://www.haomeiwen.com/subject/dnzrjttx.html