Deploy multiple uitableviews on UIScrollView

Preface

We often have to deploy multiple uitableviews on the UIScrollView. Because many interfaces of current APP can be switched by sliding

Code instance

// The core idea is very simple, which is to make the UIScrollView's canvas purchase accommodate the size of the next three uitableviews, so that you can switch among them
    _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"];

Experience

  1. Here's why we need to set the tag, because we know that when we set the cell in the - (uitableviewcell) TableView: (UITableVIew) TableView cell forrowatindexpath: (NSIndexPath *) indexpath {} function, we can only return one cell. Previously, we used to make different cells on a UITableVIew based on the judgment of section and row. Here we need to set three types of C What about ell? Judge by the tag value of TableView, so that it can return different cells
  2. The most important thing is to throw away the solid thinking learned at the beginning. The control is like a small decoration, which can be placed in any cloth
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:@"holiday\nshare Xiao Bai\n Original-Illustration-Exercise exercise\n15 Minutes ago", @"Appreciation of foreign albums\nshare Xiao Wang\n Graphic Artist Designer-Brochure Design\n16 Minutes ago", @"collection flat design \nshare Xiao Lu\n Graphic Artist Designer-Poster Design\n17 Minutes ago", @"Font story\nshare Minor law\n Design article-Design viewpoint\n18 Minutes ago", @"Plate finishing:\n Efficient solution\nshare Xiao Wang\n Graphic Artist Designer-Layout\n18 Minutes ago", 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;
}

Posted by Anco on Fri, 10 Jan 2020 07:31:21 -0800