2009-06-06 44 views
28

出於性能考慮,通常重用UITableView的單元格。 有沒有辦法用TableView標題的視圖做同樣的事情? 我說的是與委託的方法返回的那些:可重複使用TableView標題視圖

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

我試圖做這似乎並不奏效以下預期:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    static NSString *CellIdentifier = @"Header"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier]; 
    if (cell == nil) { 
     cell = [self getHeaderContentView: CellIdentifier]; 
    } 
    return cell; 
} 

有沒有一種辦法重用標題'的意見?

+1

作爲iOS 6中的你現在有`UITableViewHeaderFooterView`這使得使用reuseIdentifier的。 – h4xnoodle 2013-08-22 02:50:06

+0

但是目前您不能在UI Builder中使用UITableViewHeaderFooterView。 – 2013-09-04 18:11:44

回答

32

蘋果建立在重用tableview單元格的能力的原因是,雖然tableview可能有很多行,但屏幕上只顯示少數幾行。應用程序可以重用已存在的單元格,並根據需要重新配置它們,而不是爲每個單元格分配內存。

首先,標題視圖只是UIViews,雖然UITableViewCell是UIView的子類,但它們不能作爲節標題的視圖放置。另外,由於通常將比總行少得多的段標題,所以沒有必要構建可重用機制,事實上Apple沒有爲通用UIViews實現一個。

請注意,如果您只是爲標題設置標籤,則可以使用-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section代替。

對於某種特定問題,如用紅色文本(或按鈕,圖像等)的標籤,你可以做這樣的事情:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)] autorelease]; 
    UILabel *label = [[[UILabel alloc] initWithFrame:headerView.frame] autorelease]; 
    label.textColor = [UIColor redColor]; 
    label.text = [NSString stringWithFormat:@"Section %i", section]; 

    [headerView addSubview:label]; 
    return headerView; 
} 
+0

我認爲標籤不應該autorelease,但它已被添加到headerView後發佈。你在想什麼? – gsempe 2011-07-15 08:17:19

10

一個簡單而有效的解決方案:

@interface SectionedTableViewController() 
    @property (nonatomic, strong) UINib*   sectionHeaderNib; 
    @property (nonatomic, strong) NSMutableArray* sectionHeaders; 
@end 

@implementation SectionedTableViewController 

@synthesize sectionHeaderNib = sectionHeaderNib_; 
@synthesize sectionHeaders = sectionHeaders_; 

- (void) viewDidUnload 
{ 
    self.sectionHeaders = nil; 
    [super viewDidUnload]; 
} 

- (NSMutableArray*) sectionHeaders 
{ 
    if (!sectionHeaders_) 
     self.sectionHeaders = [NSMutableArray array]; 
    return sectionHeaders_; 
} 


- (UINib*) sectionHeaderNib 
{ 
    if (!sectionHeaderNib_) 
     self.sectionHeaderNib = [UINib nibWithNibName: NSStringFromClass(YourHeaderView.class) bundle: nil]; 
    return sectionHeaderNib_; 
} 


- (YourHeaderView*) dequeueHeader 
{ 
    return [self.sectionHeaders firstObjectPassingTest: ^(id obj) { return (BOOL) ([obj superview] == nil); }]; 
} 


- (NSString*) sectionHeaderTitleForSection: (NSInteger) section 
{ 
    return nil; 
} 


- (UIView*) tableView: (UITableView*) tableView viewForHeaderInSection: (NSInteger) section 
{ 
    YourHeaderView* headerView = [self dequeueHeader]; 
    if (!headerView) 
    { 
     headerView = [YourHeaderView instanceFromNib: self.sectionHeaderNib]; 
     [self.sectionHeaders addObject: headerView]; 
    } 
    return headerView; 
} 

@end 
7

您可以通過創建UITableViewHeaderFooterView 類它是子類的UIView實現該 您還需要創建一個INDI個人XIB,因爲它不會自動創建與UITableViewHeaderFooterView創建。

註冊筆尖用的tableview

[self.tblCart registerNib:[UINib nibWithNibName:@"CartHeaderView" bundle:nil] forHeaderFooterViewReuseIdentifier:@"CartHeader"]; 

現在您可以在viewForHeaderInSection

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{ 

    CartHeaderView *sectionHeader=[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CartHeader"]; 
return sectionHeader; 
} 

注: 要設置背景顏色,你需要創建一個相同的幀爲第一個子視圖標題併爲該視圖設置顏色。

你可以按照

Changing the background color on a UITableViewHeaderFooterView loaded from a xib says to use contentView.backgroundColor instead