簡體   English   中英

如何在UIScrollview中嵌入UITableView

[英]How to embed a UITableView in a UIScrollview

如何在UIViewController中執行以下操作(包含tableview作為子視圖)

我最初有一個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)我可以向上滾動整個頁面,即預覽部分和tableview將向上滾動2)當搜索欄到達頂部(導航欄下方)時,整個滾動將停止(參見屏幕截圖),但UITableView中的內容仍然是可滾動的

在此輸入圖像描述

編輯:

添加了UIScroll視圖的代碼。 但是,對我來說沒有任何改變。 我的代碼出了什么問題?

//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就足夠了

  1. 將“預覽視圖”設置為tableView的HeaderView

  2. 將“搜索欄”設為tableView的Section Header視圖

你可以做到完美:)

不需要任何花哨或自定義,使用UITableView並將您的預覽窗格設置為tableHeaderView並將您的搜索字段設置為使用tableView:viewForHeaderInSection:UITableViewDelegate的節頭。

在滾動事件期間,表頭將從頂部滾動。 如果可見,那么節標題在整個時間段內浮動並保持可見。 如果你只有1個部分,那么部分標題將一直存在。

或者您可以執行以下步驟:1。禁用滾動tableView。 2.將高度約束設置為tableView和連接到它的IBOutlet。 3.在重新加載數據之前,計算表格高度。 heightConstraint.constant = (heightOfTheSingleTableViewCell) * numOfRows

您必須在UIScrollView中嵌入UITableView,其大小與屏幕一樣減去導航欄。 將UIScrollViews背景設置為clearColor,以允許UITableView上方的內容保持可見。

是的,您可以使用“tableView:viewForHeaderInSection”。

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

它無法滾動,因為contentSize不夠大。 您需要將內容的長度增加到scrollView頂部的內容的總長度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM