TableView nested collectionView implementation


Sometimes we will meet such demand in the practical project, the list contains more than one line model data show, requires sliding around cell or wrap on display here, we can think about using tableView nested collectionView to realize the function of

TableView nested collectionView implementation
tableView nested collectionView.gif

I’d like to introduce the horizontal scroll collectionView

First, create tableView, set the proxy, not much to say

UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake (0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain]; tableView.delegate = self; tableView.dataSource = self; self.tableView = tableView; [self.view addSubview: self.tableView];
Implementing proxy methods
- (NSInteger) tableView: (UITableView *) tableView numberOfRowsInSection: (NSInteger) section{return 100;} - (UITableViewCell *) tableView: (* UITableView) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath{HZTableViewCell *cell [tableView dequeueReusableCellWithIdentifier:@ = "HZTableViewCell"]; if (cell = = Nil) {cell = [[HZTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@ "HZTableViewCell"]; cell.delegate = self; cell.selectionStyle = UITableViewCellSelectionStyleNone;} cell.textLabel.text [NSString stringWithFormat:@ = "%zd" indexPath.row+1]; return cell;} - (CGFloat) tableView: (UITableView * tableView) heightForRowAtIndexPath: (NSIndexPath *) indexPath{return 200;}

Two, custom tableViewCell

Add a collectionView to cell and become its agent
- (instancetype) initWithStyle: (UITableViewCellStyle) style reuseIdentifier: (NSString *) reuseIdentifie{if (self [super = initWithStyle:style reuseIdentifier:reuseIdentifie]) {[self setView]}; return self;} - (void) setView{UICollectionViewFlowLayout *layout [[UICollectionViewFlowLayout = alloc]init]; layout.minimumLineSpacing = 40; / / lateral sliding layout.scrollDirection = UICollectionViewScrollDirectionHorizontal; UICollectionView = *collectionView (50, 0, [[UICollectionView alloc]initWithFrame:CGRectMake [UIScreen mainScreen].bounds.size.width-50, 200 collectionViewLayout:layout]); collectionView.delegate = self; collectionView.dataSource = self; collectionView.backgroundColor = [UIColor purpleColor]; [self.contentView addSub View:collectionView]; [collectionView, registerClass:[HZCollectionViewCell, class], forCellWithReuseIdentifier:@, "cell"]}
Implementing proxy methods
- (NSInteger) collectionView: (UICollectionView *) collectionView numberOfItemsInSection: (NSInteger) section{return self.dataSource.count;} - (UICollectionViewCell *) collectionView: (* UICollectionView) collectionView cellForItemAtIndexPath: (NSIndexPath indexPath{HZCollectionViewCell *cell =[collectionView *) dequeueReusableCellWithReuseIdentifier:@ "cell" forIndexPath:indexPath]; cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@ "% ld.jpg", "%zd. stringWithFormat:@ cell.label.text indexPath.row+1]]; =[NSString% @" indexPath.row+1, return, self.dataSource[indexPath.row]]; cell;}
To implement a click of a item, pass the location of the item to the controller to do something convenient (I’m using the proxy method)

Define an agreement

#import < Foundation/Foundation.h> @class HZTableViewCell; @protocol HZCollectionViewDelegate < NSObject> - (void) hzcollectionView: (HZTableViewCell * hzTableViewCell) didSelectItemAtIndexPath: (NSIndexPath * collectionView); @end

In the collectionView click event, the agent method is executed, so when I create the HZTableViewCell on top, I write to cell.delegate = self;

- (void) collectionView: (UICollectionView * collectionView) didSelectItemAtIndexPath: (NSIndexPath *) indexPath{if (_delegate) {[_delegate hzcollectionView:self didSelectItemAtIndexPath:indexPath]}};

Then you need to implement the agent method in the controller

- (void) hzcollectionView: (HZTableViewCell * hzTableViewCell) didSelectItemAtIndexPath: (NSIndexPath *) indexPath{NSIndexPath *tableViewIndexPath = [self.tableView indexPathForCell:hzTableViewCell]; UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@ "" message:[NSString stringWithFormat:@ "click the%ld for the%ld, tableViewIndexPath.row+1, indexPath.row+1] preferredStyle:UIAlertControllerStyleAlert]; [self.navigationController presentViewController:alertVC animated:YES completion:^{dispatch_after (dispatch_time (DISPATCH_TIME_NOW (int64_t) (0.5 * NSEC_PER_SEC (dispatch_get_main_queue)), [alertVC dismissViewControllerAnimated:YES completion:^{), ^{}]);}];}};

Three, custom collectionViewCell not much said, according to the needs of their own design

This allows simple tableView nesting collectionView to be implemented