[英]second table view is not appearing in scroll view
I am having two table views (table, table2) in scroll view. 我在滚动视图中有两个表视图(表,表2)。 I want to scroll the table view left to right and right to left.
我想从左到右和从右到左滚动表格视图。 in this below code 1st table view (table) appears, but when I swipe right to left 2nd table view (table2) is not appearing there.
在下面的代码中,出现了第一个表格视图(表格),但是当我向右滑动时,第二个表格视图(表格2)没有出现。 blank screen appears there.
出现空白屏幕。 please help me in coding.
请帮助我编码。
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollview * theScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
theScrollView.contentSize = CGSizeMake(320*2, self.theScrollView.frame.size.height);
theScrollView.delegate = self;
theScrollView.bounces = YES;
theScrollView.showsVerticalScrollIndicator = YES;
theScrollView.showsHorizontalScrollIndicator = YES;
[self.view addSubview:theScrollView];
UITableView * table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table.contentSize=CGSizeMake(320,200);
[table setDataSource:self];
[table setDataSource:self];
[table setDelegate:self];
[table setTranslatesAutoresizingMaskIntoConstraints: YES];
table.tag = 100;
[theScrollView addSubview:self.table];
UITableView * table2 = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
table2.contentSize=CGSizeMake(320,200);
[table2 setDataSource:self];
[table2 setDelegate:self];
table2.tag = 101;
[theScrollView addSubview:self.table2];
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.theScrollView = nil;
self.table = nil;
self.table2 = nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (tableView.tag == 100) {
return 3;
}
if (tableView.tag == 101) {
return 4;
}
return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView.tag == 100) {
return 5;
}
if (tableView.tag == 101) {
return 5;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView.tag == 100) {
return [NSString stringWithFormat:@"%@", @"Plain"];
}
if (tableView.tag == 101) {
return [NSString stringWithFormat:@"%@", @"Group"];
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.tag == 100) {
static NSString *cellIdentifier1 = @"cellIdentifier1";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier1] ;
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
cell.textLabel.text = @"45";
return cell;
}
if (tableView.tag == 101) {
static NSString *cellIdentifier2 = @"cellIdentifier2";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier2] ;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = @"45";;
return cell;
}
return nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
float x = scrollView.contentOffset.x;
if (x > 320/2 && x < 640/2) {
self.title = @"TableView Group";
}
if (x > 0 && x < 320/2) {
self.title = @"TableView Plain";
[self.table reloadData];
}
}
According to your code, you setting equal frame for both table views: 根据您的代码,为两个表视图设置相等的框架:
CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
So by adding them to scroll view, you just placing them on top of each other. 因此,通过将它们添加到滚动视图中,您只需将它们放置在彼此之上。
Try to change the frame for second table view: 尝试更改第二个表格视图的框架:
CGRectMake(320, 0, self.view.frame.size.width, self.view.frame.size.height)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.