我有一個使用所有默認UITableView
東西創建的視圖,但現在我需要在UITableView
之上添加標題區域(所以UITableView
將正常滾動,但前100px屏幕左右都會有靜態標題內容)。我沒有看到我可以在IB中調整UITableView
的位置,但我不確定如何執行此操作。帶有標題區域的iPhone UITableView
有誰知道嗎?
我有一個使用所有默認UITableView
東西創建的視圖,但現在我需要在UITableView
之上添加標題區域(所以UITableView
將正常滾動,但前100px屏幕左右都會有靜態標題內容)。我沒有看到我可以在IB中調整UITableView
的位置,但我不確定如何執行此操作。帶有標題區域的iPhone UITableView
有誰知道嗎?
爲什麼不使用UITableView提供的標題?如下所示:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return @"My Title";
}
此外,您可以通過拖動邊框來調整IB中的表格視圖的大小。
您必須將UITableView
與UIView
一起嵌入另一個視圖(您將其稱爲標題部分)。
因此,UIView
將有2個子視圖。標題視圖後跟表格視圖。
UIView
(父)
UIView
(標題)UITableView
(表)希望這有助於。
當您添加UIView
或它的一個子上使用IB的UITableView
(只需拖動一個UIView其拖放到的你的UITableView
的上部),它會自動將UIView
組件,並將其作爲「的tableHeader」零件。
每個UITableView
有一個tableHeader
和保留一個tableFooter
組件...
這樣的新觀點將是UITable的一部分,並與它滾動或出現/消失或不管你做什麼表。如果您需要條件行爲,您可以更改其隱藏屬性。
在另一方面,如果你想在頭視圖原地不動,作爲表滾動,那麼最好是使表更小,並把它上面的標題在其他的答案建議...
您可以使用UITableViewDelegate
方法爲表創建自定義標題視圖並指定高度,即tableView:viewForHeaderInSection:
和tableView:heightForHeaderInSection:
。你可以添加任何你喜歡的視圖。下面是增加了一個右對齊UILabel
一個例子:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,30)];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, headerView.frame.size.width-120.0, headerView.frame.size.height)];
headerLabel.textAlignment = NSTextAlignmentRight;
headerLabel.text = [titleArray objectAtIndex:section];
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
,'UITextAlignment'已過時,使用'NSTextAlignment'代替 –
感謝@indienchild - 我已經更新了它ARC和iOS 7 –
謝謝你...... !!!! –
我喜歡noodl_es(upvoted)的答案,因爲它提供了你需要的功能和行爲,但你不必擔心調整大小的UITableView:這是自動處理的。但是,解決方案最適合於僅當表頭信息專用於表格的第一部分時(或者表格只有一個部分)。如果表格有多個部分,則第二部分的標題在向上滾動時將推開第一部分的標題,因此標題視圖不會與整個表格相關。
感謝您的支持!您不能使用section變量來指定您的自定義頁眉視圖出現在哪個部分? –
@noodl_es我覺得OP試圖添加靜態(「頭」)鑑於在這個意義上相同的觀點應該是可見不管表是如何滾動。我的理解是,該視圖應該基本上獨立於表視圖,所以這將是在UITableView上添加一個獨立的UIView的問題。我可能是錯的,這是很久很久以前(我花了5分鐘就承認什麼,我已經張貼在這裏:-) – rene
假設有你UITableViewController
@interface MXMTableViewController : UITableViewController <UITableViewDelegate,UIScrollViewDelegate> {
/// your table view interface here
}
和你簡單UITableView
一個廈門國際銀行在它尚未定義,你可以做米希爾說重寫loadView
方法是這樣的:
- (void)loadView {
[super loadView];
UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view = mainView;
[mainView release];
// Add Header View
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 36)];
headerView.backgroundColor = [UIColor redColor];
[self.view addSubview:headerView];
// now, move your table view down. Check you nib to choose
// the right Y-axis offset
CGRect f = tableView.frame;
f.origin.y += headerView.frame.size.height/2;
tableView.frame = f;
// Add the table view to the container view
[self.view addSubview:self.tableView];
// Add footer
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.frame.size.height, 320, 125)];
footerView.backgroundColor = [UIColor redColor];
[self.view addSubview:footerView];
[footerView release];
[headerView release];
}
..就是這樣。你有一個UITableView
固定的頁眉和頁腳。
PS。您現在可以使用您的xib自定義視圖作爲頁眉和頁腳的視圖。
f.origin.y + = headerView.frame.size.height/2; ?我認爲它不會被兩個分開,它是** f.origin.y + = headerView.frame.size。身高; ** – SpaceDog
Found a solution at iphonedevsdk
而不是做這個的:
[tableViewController.view addSubview:viewSubclass];
做到這一點
[tableViewController.navigationController.view addSubview:viewSubclass];
我終於解決了這個問題不改變的基類的正確方法。將視圖添加到父導航控制器的答案很好,但轉換看起來很糟糕。
的修補程序實際上是容易的。訣竅是爲self.tableView屬性創建自定義setter和getter。然後,在loadView中,用新的UIView替換視圖並將tableView添加到它。然後你可以自由地在tableView中添加子視圖。下面是它是如何做:
在標題:
@interface CustomTableViewController : UITableViewController
{
UITableView *tableView;
}
在.M:
- (UITableView*)tableView
{
return tableView;
}
- (void)setTableView:(UITableView *)newTableView
{
if (newTableView != tableView)
{
[tableView release];
tableView = [newTableView retain];
}
}
- (void)loadView {
[super loadView];
//save current tableview, then replace view with a regular uiview
self.tableView = (UITableView*)self.view;
self.view = [[UIView alloc] initWithFrame:self.tableView.frame];
[self.view addSubview:self.tableView];
//code below adds some custom stuff above the table
UIView *customHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
customHeader.backgroundColor = [UIColor redColor];
[self.view addSubview:customHeader];
[customHeader release];
self.tableView.frame = CGRectMake(0, customHeader.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - customHeader.frame.size.height);
}
- (void)viewDidUnload
{
self.tableView = nil;
[super viewDidUnload];
}
享受!
這太棒了!感謝這個有用的提示! – SparkyNZ
這只是在iOS 6上爲我繪製黑屏。 –
我需要把比文本標頭的UITableView提供了更多。這將是一個100px左右的高區域,左邊是一個人的照片,右邊是一些細節。標題下方的表格視圖將是該人員發佈到公司博客的列表。所以這就是爲什麼我需要一個與UITableView分開的標題區域。 –
那麼最好的辦法是讓桌子變小。確保視圖控制器屬性「視圖」是一個實際的UIView,而不是一個UITableView這樣你可以先拖一個UITableView到UIView的,然後拖動邊框到所需的大小。 –
仍然,爲tableView:viewForHeaderInSection提供您的自定義UIView:可能會在這種情況下工作! –