博采众长
只看不敲只会原地踏步,只敲不看也是蒙眼走路.
image.png
和双tableVeiw联动类似,在点击tableView联动collectionView时有不同.具体请看代码
#import "TCViewController.h"
#import "ZCCollectionViewCell.h"
@interface TCViewController ()<UITableViewDelegate, UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic , strong) UITableView *leftTableV;
@property (nonatomic , strong) UICollectionView *rightCollection;
@property (nonatomic , copy) NSArray *leftAry;
@property (nonatomic , copy) NSArray *rightAry;
@property (nonatomic , assign) BOOL isToDown;//向下滑
@end
@implementation TCViewController
- (void)viewDidLoad {
[super viewDidLoad];
_leftAry = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
self.rightAry = @[@[@"123",@"11",@"11111",@"11223344",@"11111",@"11223344",@"12345678",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"],@[@"123",@"11",@"11111",@"11223344",@"12345678"]];
[self.view addSubview:self.leftTableV];
[self.view addSubview:self.rightCollection];
// Do any additional setup after loading the view.
}
- (UITableView *)leftTableV
{
if (!_leftTableV) {
_leftTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 100, self.view.frame.size.height) style:UITableViewStylePlain];
_leftTableV.backgroundColor = [UIColor redColor];
_leftTableV.dataSource = self;
_leftTableV.delegate = self;
[_leftTableV registerClass:[UITableViewCell class] forCellReuseIdentifier:@"left"];
}
return _leftTableV;
}
- (UICollectionView *)rightCollection
{
if (!_rightCollection) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumInteritemSpacing = 3;
// CGFloat w = ( self.view.frame.size.width - 100) / 2;
// layout.itemSize = CGSizeMake(w, w);
_rightCollection = [[UICollectionView alloc] initWithFrame:CGRectMake(100, 0, self.view.frame.size.width - 100, self.view.frame.size.height) collectionViewLayout:layout];
_rightCollection.backgroundColor = [UIColor greenColor];
_rightCollection.delegate = self;
_rightCollection.dataSource = self;
[_rightCollection registerClass:[ZCCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
[_rightCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
}
return _rightCollection;
}
#pragma mark tableveiw
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.leftAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"left" forIndexPath:indexPath];
cell.textLabel.text = self.leftAry[indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.leftTableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.row inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
//[self.rightCollection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:indexPath.row] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];//直接跳会不准,具体原因请大神解疑
//重点
NSIndexPath *rIndexPath = [NSIndexPath indexPathForItem:0 inSection:indexPath.row];//获取要滚动的区
//获取此区的布局属性
UICollectionViewLayoutAttributes *att = [self.rightCollection.collectionViewLayout layoutAttributesForSupplementaryViewOfKind:UICollectionElementKindSectionHeader atIndexPath:rIndexPath];
// 滚动位置
CGPoint top = CGPointMake(0, att.frame.origin.y - self.rightCollection.contentInset.top);
[self.rightCollection setContentOffset:top animated:YES];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
static CGFloat offSetY = 0;
if ((UICollectionView *)scrollView == _rightCollection) {
self.isToDown = offSetY < scrollView.contentOffset.y;//xiangxia
offSetY =scrollView.contentOffset.y;
}
}
#pragma mark - collectionview
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return ((NSArray *)self.rightAry[section]).count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return self.rightAry.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ZCCollectionViewCell *item = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
item.textLB.text = self.rightAry[indexPath.section][indexPath.row];
item.backgroundColor =[UIColor cyanColor];
return item;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
for (UIView *view in header.subviews) {
[view removeFromSuperview];
}
UILabel *textLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.rightCollection.frame.size.width, 30)];
textLB.text = self.leftAry[indexPath.section];
[header addSubview:textLB];
header .backgroundColor = [UIColor yellowColor];
return header;
}
return nil;
}
#pragma mark - layout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
return CGSizeMake(self.rightCollection.frame.size.width, 30);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGFloat w = ( self.rightCollection.frame.size.width - 3 * 3) / 2;
return CGSizeMake(w, w);
}
#pragma mark - tableveiw 联动
//区头将出现
- (void)collectionView:(UICollectionView *)collectionView willDisplaySupplementaryView:(UICollectionReusableView *)view forElementKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
//向上而且是用户滚动
if (!self.isToDown && (collectionView.dragging || collectionView.decelerating)) {
if (indexPath.section == 0) {
[self.leftTableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.leftTableV selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}else
{
[self.leftTableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section - 1 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.leftTableV selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section -1 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
}
}
//区头展示结束
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingSupplementaryView:(UICollectionReusableView *)view forElementOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath{
//向下滑
if (self.isToDown && (collectionView.dragging || collectionView.decelerating)) {
[self.leftTableV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
[self.leftTableV selectRowAtIndexPath:[NSIndexPath indexPathForRow:indexPath.section inSection:0] animated:YES scrollPosition:UITableViewScrollPositionTop];
}
}
网友评论