#import <UIKit/UIKit.h>
#import "WSCollectionViewCell.h"
@interface WSCollectionView : UICollectionView
@property (nonatomic, strong) NSIndexPath *indexPath;
@end
static NSString *CollectionViewCellIdentifier = @"CollectionViewCellIdentifier";
@interface WSTableViewCell : UITableViewCell
@property (nonatomic,strong)WSCollectionView *collectionView;
@property (nonatomic,strong)UIImageView *itemImageView;
@property (nonatomic,strong)UILabel *contentLabel;
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath;
- (void)configureCellWithInfo:(NSDictionary *)data;
@end
#import "WSTableViewCell.h"
@implementation WSCollectionView
@end
@implementation WSTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
[self initialize];
}
return self;
}
- (void)initialize
{
self.selectionStyle = UITableViewCellSelectionStyleNone;
CGFloat tempImageWith = [UIScreen mainScreen].bounds.size.width / 640;
CGFloat tempHeight = 480 * tempImageWith;
self.itemImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, tempHeight)];
self.itemImageView.contentMode = UIViewContentModeScaleAspectFill;
self.itemImageView.clipsToBounds = YES;
[self.contentView addSubview:_itemImageView];
self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _itemImageView.frame.size.height + 10, [UIScreen mainScreen].bounds.size.width - 20, 40)];
self.contentLabel.font = [UIFont systemFontOfSize:16];
self.contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.contentLabel.numberOfLines = 0;
[self.contentView addSubview:_contentLabel];
/********************************************/
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
layout.itemSize = CGSizeMake(85, 85 + 45);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
self.collectionView = [[WSCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
self.collectionView.frame = CGRectMake(0, tempHeight + 10 + 50 + 5, [UIScreen mainScreen].bounds.size.width, 85 + 45);
[self.collectionView registerClass:[WSCollectionViewCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor whiteColor];
self.collectionView.showsHorizontalScrollIndicator = NO;
UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, tempHeight + 205, [UIScreen mainScreen].bounds.size.width, 10)];
lineView.backgroundColor = [UIColor colorWithRed:0.8755 green:0.8755 blue:0.8755 alpha:1.0];
[self.contentView addSubview:lineView];
[self.contentView addSubview:self.collectionView];
}
- (void)configureCellWithInfo:(NSDictionary *)data
{
self.itemImageView.image = [UIImage imageNamed:[data objectForKey:@"imageName"]];
self.contentLabel.text = [data objectForKey:@"content"];
}
- (void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate indexPath:(NSIndexPath *)indexPath
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.indexPath = indexPath;
[self.collectionView setContentOffset:self.collectionView.contentOffset animated:NO];
[self.collectionView reloadData];
}
#pragma mark -- other
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
#import <UIKit/UIKit.h>
@interface WSCollectionViewCell : UICollectionViewCell
@property (nonatomic,strong)UILabel *goodsNameLabel;
@property (nonatomic,strong)UIImageView *goodsImageView;
- (void)configureCellWithInfo:(NSDictionary *)data;
@end
#import "WSCollectionViewCell.h"
@implementation WSCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initialize];
}
return self;
}
- (void)initialize
{
self.backgroundColor = [UIColor whiteColor];
self.goodsImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 85, 85)];
self.goodsImageView.backgroundColor = [UIColor whiteColor];
self.goodsNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 85 + 5, 85, 40)];
self.goodsNameLabel.textColor = [UIColor blackColor];
self.goodsNameLabel.numberOfLines = 2;
self.goodsNameLabel.font = [UIFont systemFontOfSize:14];
self.goodsNameLabel.textAlignment = NSTextAlignmentCenter;
[self.contentView addSubview:self.goodsImageView];
[self.contentView addSubview:self.goodsNameLabel];
}
//set data
- (void)configureCellWithInfo:(NSDictionary *)data
{
self.goodsImageView.image = [UIImage imageNamed:[data objectForKey:@"imageName"]];
self.goodsNameLabel.text = [data objectForKey:@"goodsName"];
}
@end
#import "ViewController.h"
#import "WSTableViewCell.h"
#import "WSCollectionViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableDictionary *contentOffsetDictionary;
@property (nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"Detail";
NSMutableArray *tempArray = [NSMutableArray array];
for (int i = 0; i < 10; i ++) {
NSString *imageName = [NSString stringWithFormat:@"00%d.png",i + 1];
NSString *contentStr = [NSString stringWithFormat:@"The best preparation for tomorrow is doing your best today. current row %d",i + 1];
NSString *goodsImageName = [NSString stringWithFormat:@"a%d.jpg",i + 1];
[tempArray addObject:@{@"imageName":goodsImageName,@"goodsName":[NSString stringWithFormat:@"default name :%d",i + 1]}];
if (!_dataArray) {
_dataArray = [[NSMutableArray alloc]init];
}
[_dataArray addObject:@{@"imageName":imageName,@"content":contentStr,@"goods":tempArray}];
}
NSLog(@"dataArray:%@",_dataArray);
[self.view addSubview:self.tableView];
}
#pragma mark -- UI
- (UITableView *)tableView
{
if (!_tableView) {
_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.separatorStyle = NO;
}
return _tableView;
}
#pragma mark -- UITableViewDelegate
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
WSTableViewCell *cell = (WSTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[WSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *dic = self.dataArray[indexPath.row];
[cell configureCellWithInfo:@{@"content":[dic objectForKey:@"content"],@"imageName":[dic objectForKey:@"imageName"]}];
return cell;
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(WSTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[cell setCollectionViewDataSourceDelegate:self indexPath:indexPath];
NSInteger index = cell.collectionView.indexPath.row;
CGFloat horizontalOffset = [self.contentOffsetDictionary[[@(index) stringValue]] floatValue];
[cell.collectionView setContentOffset:CGPointMake(horizontalOffset, 0)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat tempImageWith = [UIScreen mainScreen].bounds.size.width / 640;
CGFloat tempHeight = 480 * tempImageWith;
return tempHeight + 215;
}
#pragma mark - UICollectionViewDataSource Methods
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSArray *collectionViewArray = [self.dataArray[[(WSCollectionView *)collectionView indexPath].row]objectForKey:@"goods"];
return collectionViewArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
WSCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
NSArray *collectionViewArray = [self.dataArray[[(WSCollectionView *)collectionView indexPath].item]objectForKey:@"goods"];
NSDictionary *dic = [collectionViewArray objectAtIndex:indexPath.row];
[cell configureCellWithInfo:dic];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = [(WSCollectionView *)collectionView indexPath].row;
NSLog(@"section:%ld row%ld",section,indexPath.row);
}
#pragma mark - UIScrollViewDelegate Methods
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (![scrollView isKindOfClass:[UICollectionView class]]) return;
CGFloat horizontalOffset = scrollView.contentOffset.x;
WSCollectionView *collectionView = (WSCollectionView *)scrollView;
NSInteger index = collectionView.indexPath.row;
self.contentOffsetDictionary[[@(index) stringValue]] = @(horizontalOffset);
}
#pragma mark -- other
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end