如何在UIScrollview中嵌入UITableView

17

在一个包含表格视图的UIViewController中,如何执行以下操作?

我最初有一个UIViewController,其中显示了预览部分(UIView)。

//Setup container for preview section
UIView *tempContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 20, 320, 100)];
self.preview_answer_container = tempContainer;
self.preview_answer_container.backgroundColor = [UIColor clearColor];
[tempContainer release];
[self.view addSubview:self.preview_answer_container];

我还在下面添加了一个UITableView(和一个搜索栏)

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,44 ,320,self.view.frame.size.height - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

[self.view addSubview:self.tagFriendsTableView];

在此输入图片描述

通过这种设置,我的滚动区域很小(仅位于预览区域下面的静态区域)。

如何修改我的代码以实现以下功能: 1)我可以向上滚动整个页面,即预览部分和表格视图一起向上滚动。 2)当搜索栏到达顶部(导航栏下方)时,整体滚动停止(参见屏幕截图),但UITableView中的内容仍然可滚动。

在此输入图片描述

编辑:

添加了UIScrollView的代码。 然而,对我来说没有任何变化。 我的代码有什么问题吗?

//setup scroll view
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

//Search
UISearchBar *tempBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 120 + 20, 320, 44)];
self.sBar = tempBar;
[tempBar release];
self.sBar.delegate = self;
self.sBar.tintColor = [UIColor colorWithHexString:@"#b6c0c7"];
self.sBar.placeholder = @"Search for FB and DM friends";

[scroll addSubview:sBar];

//setup tableview
UITableView *tempTable = [[UITableView alloc]initWithFrame:CGRectMake(0,144 + 20 + 20 ,320,self.view.frame.size.height - 144 - 20 - 20 - 44)];
self.tagFriendsTableView = tempTable;
self.tagFriendsTableView.delegate = self;
self.tagFriendsTableView.dataSource = self;
[tempTable release];

//Add the scroll view to the parent view
[scroll addSubview:self.tagFriendsTableView];

scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
6个回答

33

我有一个解决方案,

只使用tableView就足够了

  1. 将“预览视图”作为tableView的HeaderView

  2. 将“搜索栏”作为tableView的Section Header View

你可以完美地实现它:)


1
有人可以再为我解释一下吗?请。 - Ravi Ojha
@RaviOjha,请看下面DBD的回答。 - andrewlundy

4
不需要什么花哨或定制,只需使用一个UITableView,将预览窗格设置为tableHeaderView,并使用tableView:viewForHeaderInSection:UITableViewDelegate中将您的搜索栏设置为部分标题。
表头会在滚动事件中向上滚动。 部分标题漂浮并保持可见,整个时间段都是可见的。 如果只有1个部分,则该部分标题将一直存在。

2

或者你可以按照以下步骤操作: 1. 禁用tableView的滚动。 2. 为tableView设置高度约束,并连接到IBOutlet。 3. 在刷新数据之前,计算表格的高度。heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows


1
您需要将UITableView嵌入到一个UIScrollView中,该UIScrollView的大小为屏幕大小减去导航栏的高度。将UIScrollViews的背景设置为clearColor,以使UITableView上方的内容保持可见。

你好,感谢您的回复。我添加了一些UIScrollView的代码,但是它对我没有起作用,也就是说与我的原始实现没有任何变化。您能否帮我看一下我编辑后的问题?我在那里添加了我的UIScrollView代码。提前致谢。 - Zhen
你应该选择DBD/adali的解决方案,它比我的好 :) - rhummelmose

1

是的,你可以使用"tableView:viewForHeaderInSection"。


0
scroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);

无法滚动,因为contentSize不够大。您需要将长度增加到scrollView顶部的所有内容的总长度。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接