[英]Setting header for only one uitableview
我需要幫助。 我在一個UIView
有三個UITableViews
。 但是,我只希望一個UITableView
具有標題。 我怎么用代碼說呢? 我已經有以下代碼。 多謝你們。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == Table1) {
return @"My Header Text";
} else if (tableView == Table2) {
return nil;
} else if (tableView == Table3) {
return nil;
}
}
您提供的代碼將僅添加特定部分的標題視圖。 因此,如果您有兩個或更多部分,則需要編寫額外的邏輯來設置每個部分的標題視圖。
這是為單個表設置tableHeaderView
一種簡單方法。
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @"my table header";
table1.tableHeaderView = label;
如果這不起作用,您還可以嘗試為標題的高度返回零:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (tableView == Table1)
{
return 50.0; // or what's the height?
}
else
{
return 0.0;
}
}
嘗試這個
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(20, 6, 300, 30);
label.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view addSubview:label];
return view;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.