如何在 UIScrollview 中嵌入 UITableView [英] How to embed a UITableView in a UIScrollview
问题描述
如何在 UIViewController 中执行以下操作(包含 tableview 作为子视图)
How can I do the following in a UIViewController (containing a tableview as a subview)
我最初有一个 UIViewController 显示预览部分 (UIView)
I initially have a UIViewController showing a preview section (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(和一个搜索栏)
I also added a UITableView (and a searchbar) below
//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];
使用此设置,我的滚动区域很小(预览部分下方只有静态区域)
With this setup, my scrolling area is small (only static area below preview section)
我如何修改我的代码,以便1) 我可以向上滚动整个页面,即预览部分和 tableview 将一起向上滚动2)当搜索栏到达顶部(导航栏下方)时,整体滚动将停止(见截图),但UITableView中的内容仍然可以滚动
How can I modify my code such that 1) I can scroll the whole page upwards i.e. preview section and tableview will scroll up together 2) Scrolling as a whole will stop when the search bar reaches the top (below navbar) (see screenshot), but the contents in the UITableView is still scrollable
为 UIScroll 视图添加了代码.然而,对我来说什么都没有改变.我的代码有什么问题?
Added code for UIScroll view. However, nothing changed for me. What is wrong with my code?
//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];
推荐答案
我有一个解决方案,
只用tableView就够了
use only tableView is enough
将预览视图"设为 tableView 的 HeaderView
make the "Preview View" as the HeaderView of your tableView
将搜索栏"作为 tableView 的部分标题视图
make the "Search Bar" as the Section Header View of your tableView
你可以做到完美:)
这篇关于如何在 UIScrollview 中嵌入 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!