前言
我们经常遇到要在UIScrollView上部署多个UITableView的情况。因为现在的APP很多界面是可以滑动切换的
代码实例
// 核心思路非常简单,就是让UIScrollView的画布内购容纳下3个UITableView的大小,这样就可以在其中切换了
_scrollView.bounces = NO;
_scrollView.alwaysBounceHorizontal = NO;
_scrollView.alwaysBounceVertical = NO;
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 30, 320, 450)];
_scrollView.contentSize = CGSizeMake(320 * 3, 450);
_scrollView.delegate = self;
_scrollView.pagingEnabled = YES;
_scrollView.scrollEnabled = YES;
_scrollView.bounces = YES;
_scrollView.bouncesZoom = NO;
_scrollView.showsHorizontalScrollIndicator = NO;
[self.view addSubview:_scrollView];
_tableView1 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 450) style:UITableViewStyleGrouped];
_tableView1.delegate = self;
_tableView1.dataSource = self;
_tableView1.tag = 1; // 1
[self.scrollView addSubview:_tableView1];
_tableView2 = [[UITableView alloc] initWithFrame:CGRectMake(320, 0, 320, 450) style:UITableViewStyleGrouped];
_tableView2.delegate = self;
_tableView2.dataSource = self;
_tableView2.tag = 2;
[self.scrollView addSubview:_tableView2];
_tableView3 = [[UITableView alloc] initWithFrame:CGRectMake(640, 0, 320, 450) style:UITableViewStyleGrouped];
_tableView3.delegate = self;
_tableView3.dataSource = self;
_tableView3.tag = 3;
[self.scrollView addSubview:_tableView3];
[_tableView1 registerClass:[ShareInformationRecommendTableViewCell class] forCellReuseIdentifier:@"cell1"];
[_tableView2 registerClass:[ShareInformationRecommendTableViewCell class] forCellReuseIdentifier:@"cell2"];
[_tableView3 registerClass:[ShareInformationRecommendTableViewCell class] forCellReuseIdentifier:@"cell3"];
心得
- 这里说一下为什么要设置tag,是因为我们知道,在- (UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { }函数里设置cell的时候我们只能返回一种cell,之前都是根据section与row的判断使得一个UITableVIew上可以有不同的cell,可这里我们要设置3种cell怎么办?就通过TableView的tag值进行判断,使它能够返回不同的cell
- 最主要的还是把一开始学的固化思维丢掉,控件就好像小装饰品,放在哪个布都可以
if (tableView.tag == 1) {
ShareInformationRecommendTableViewCell *cell1 = nil;
cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"forIndexPath:indexPath];
NSArray *button1Image = [NSArray arrayWithObjects:[UIImage imageNamed:@"list_img1"], [UIImage imageNamed:@"list_img2"], [UIImage imageNamed:@"list_img3"], [UIImage imageNamed:@"note_img3"], [UIImage imageNamed:@"list_img4"], nil];
NSArray *label1Text = [NSArray arrayWithObjects:@"假日\nshare小白\n原创-插画-练习习作\n15分钟前", @"国外画册欣赏\nshare小王\n平面设计-画册设计\n16分钟前", @"collection扁平化设计\nshare小吕\n平面设计-海报设计\n17分钟前", @"字体故事\nshare小律\n设计文章-设计观点\n18分钟前", @"板式整理术:\n高效解决\nshare小王\n平面设计-版面设计\n18分钟前", nil];
[cell1.button1 setImage:button1Image[indexPath.section] forState:UIControlStateNormal];
cell1.label1.font = [UIFont systemFontOfSize:12];
cell1.label1.textAlignment = NSTextAlignmentLeft;
cell1.label1.numberOfLines = 0;
NSString *str = label1Text[indexPath.section];
NSArray *tempArr = [str componentsSeparatedByString:@"s"];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:str];
[AttributedStr addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:20.0]
range:NSMakeRange(0, ((NSString *)tempArr[0]).length)];
cell1.label1.attributedText = AttributedStr;
[cell1.button2 setImage:[UIImage imageNamed:@"button_zan"] forState:UIControlStateNormal];
[cell1.button2 setTitleColor:[UIColor colorWithRed:0.18f green:0.52f blue:0.77f alpha:1.00f] forState:UIControlStateNormal];
[cell1.button2 setTitle:@"25" forState:UIControlStateNormal];
[cell1.button2 setTitle:@"26" forState:UIControlStateSelected];
[cell1.button2 addTarget:self action:@selector(touchZan:) forControlEvents:UIControlEventTouchUpInside];
cell1.button2.titleLabel.font = [UIFont systemFontOfSize:11];
[cell1.button3 setImage:[UIImage imageNamed:@"button_guanzhu"] forState:UIControlStateNormal];
[cell1.button3 setTitleColor:[UIColor colorWithRed:0.18f green:0.52f blue:0.77f alpha:1.00f] forState:UIControlStateNormal];
[cell1.button3 setTitle:@"33" forState:UIControlStateNormal];
[cell1.button3 setTitle:@"34" forState:UIControlStateSelected];
[cell1.button3 addTarget:self action:@selector(touchZan:) forControlEvents:UIControlEventTouchUpInside];
cell1.button3.titleLabel.font = [UIFont systemFontOfSize:11];
[cell1.button4 setImage:[UIImage imageNamed:@"button_share"] forState:UIControlStateNormal];
[cell1.button4 setTitleColor:[UIColor colorWithRed:0.18f green:0.52f blue:0.77f alpha:1.00f] forState:UIControlStateNormal];
[cell1.button4 setTitle:@"12" forState:UIControlStateNormal];
[cell1.button4 setTitle:@"13" forState:UIControlStateSelected];
[cell1.button4 addTarget:self action:@selector(touchZan:) forControlEvents:UIControlEventTouchUpInside];
cell1.button4.titleLabel.font = [UIFont systemFontOfSize:11];
return cell1;
}