如何在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
-
将预览视图设为HeaderView你的tableView
make the "Preview View" as the HeaderView of your tableView
将搜索栏作为你的tableView的Section Header视图
make the "Search Bar" as the Section Header View of your tableView
你可以完美地完成它:)
you can make it perfectly :)
这篇关于如何在UIScrollview中嵌入UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!