效果图
demo展示针对嵌套问题.首先还是自定义UITableview的cell
然后在cell中添加一个全屏膜大小的UICollectionview
然后再对自定义UICollectionviewCell进行布局
首先自定义UITableviewCell
#import "CYRecommendTableView.h"
static NSString *const recommendWebSiteCellReuseID = @"CYRecommendWebSiteCellReuseID";
- (void)initUI {
[self registerClass:[CYWebsiteCell class] forCellReuseIdentifier:recommendWebSiteCellReuseID];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.contentType == CYRecommendContentTypeWebsit) {
CYWebsiteCell *cell = [tableView dequeueReusableCellWithIdentifier:recommendWebSiteCellReuseID forIndexPath:indexPath];
cell.webSiteCellDelegate = self;
cell.dataArray = self.modelArray;
return cell;
} else if (self.contentType == CYRecommendContentTypeTakeLookNews) {
} else {
UITableViewCell *cell = [[UITableViewCell alloc] init];
return cell;
}
}
#pragma mark - CYWebsiteCellDelegate
/// 获取UICollectionview的点击代理
- (void)websiteCellTableView_didSelectCell:(NSURL *)url indexPath:(nonnull NSIndexPath *)indexPath{
if ([self.recommendTableView_delegate respondsToSelector:@selector(recommendTableView_didSelectCell:)]) {
CYRecommendModel *model = self.modelArray[indexPath.row];
NSURL *webSiteUrl = [NSURL URLWithString:model.url];
[self.recommendTableView_delegate recommendTableView_didSelectCell:webSiteUrl];
}
}
自定义Cell中添加UICollectionView
#import "CYWebsiteCell.h"
- (void)initLayout {
UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
// 需要一行显示2个item
NSUInteger count = 2;
CGFloat margin = 15;
CGFloat itemW = (SCREEN_WIDTH - (count + 1) * margin) / 2;
CGFloat itemH = 65;
flow.itemSize = CGSizeMake(itemW, itemH);
flow.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flow];
[self.collectionView registerClass:[CYWebsiteCollectionViewCell class] forCellWithReuseIdentifier:websiteCollectionCellReuseID];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor whiteColor];
[self.contentView addSubview:self.collectionView];
}
-(void)layoutSubviews{
[super layoutSubviews];
self.collectionView.frame = self.bounds;
}
- (void)setDataArray:(NSArray *)dataArray {
_dataArray = dataArray;
[self.collectionView reloadData];
}
//设置每个item的UIEdgeInsets
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(10, 15, 20, 15);
}
//设置每个item水平间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 15;
}
//设置每个item垂直间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 20;
}
#pragma mark --UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CYWebsiteCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:websiteCollectionCellReuseID forIndexPath:indexPath];
cell.model = self.dataArray[indexPath.row];
return cell;
}
/// 代理回传出去
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
CYRecommendModel *model = self.dataArray[indexPath.row];
NSURL *url = [NSURL URLWithString:model.url];
if ([self.webSiteCellDelegate respondsToSelector:@selector(websiteCellTableView_didSelectCell:indexPath:)]) {
[self.webSiteCellDelegate websiteCellTableView_didSelectCell:url indexPath:indexPath];
}
}
最后就是自定义UICollectionCell进行布局
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self initUI];
[self initLayout];
}
return self;
}
- (void)initUI {
self.borldView = [[UIView alloc] init];
self.borldView.layer.borderWidth = 1;
self.borldView.layer.borderColor = CYCOLOR(0xB8B8B8).CGColor;
self.borldView.layer.cornerRadius = 7.5;
self.borldView.layer.masksToBounds = YES;
[self.contentView addSubview:self.borldView];
self.pictureImageView = [[UIImageView alloc] init];
self.pictureImageView.layer.cornerRadius = 22.5;
self.pictureImageView.layer.masksToBounds = YES;
[self.borldView addSubview:self.pictureImageView];
self.titleLabel = [UILabel createLabelWithText:@"-" textColor:CYCOLOR(0x333333) font:[UIFont fontWithName:@"PingFangSC-Regular" size:14] numberOfLines:0 superview:self.borldView];
self.typeNameLabel = [UILabel createLabelWithText:@"-" textColor:CYCOLOR(0x999999) font:[UIFont fontWithName:@"PingFangSC-Regular" size:13] numberOfLines:0 superview:self.borldView];
}
- (void)initLayout {
[self.borldView mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(self.contentView);
make.height.mas_equalTo(self.contentView);
}];
[self.pictureImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_offset(12);
make.top.mas_offset(10);
make.width.mas_equalTo(45);
make.height.mas_equalTo(45);
}];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.pictureImageView.mas_right).offset(12);
make.top.mas_equalTo(self.pictureImageView.mas_top);
make.height.mas_equalTo(20);
}];
[self.typeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self.titleLabel);
make.bottom.mas_equalTo(self.pictureImageView.mas_bottom);
make.height.mas_equalTo(20);
}];
}
- (void)setModel:(CYRecommendModel *)model {
_model = model;
NSURL *url = [NSURL URLWithString:model.image_url];
[self.pictureImageView sd_setImageWithURL:url placeholderImage:[UIImage imageWithBorderW:10 color:CYCOLOR(0x979797) CircleImageName:model.image_url]];
self.titleLabel.text = model.title;
self.typeNameLabel.text = model.type_name;
}
网友评论