3 回答

TA貢獻(xiàn)1773條經(jīng)驗 獲得超3個贊
我知道這個問題是針對iOS 5的,但是為了將來的讀者受益,請注意,我們現(xiàn)在可以使用有效的iOS 6 dequeueReusableHeaderFooterViewWithIdentifier代替dequeueReusableCellWithIdentifier。
因此viewDidLoad,請致電registerNib:forHeaderFooterViewReuseIdentifier:或registerClass:forHeaderFooterViewReuseIdentifier:。然后在viewForHeaderInSection,請致電tableView:dequeueReusableHeaderFooterViewWithIdentifier:。您不能使用帶有此API的單元原型(它是基于NIB的視圖或以編程方式創(chuàng)建的視圖),但這是用于出列的頁眉和頁腳的新API。

TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊
只需使用原型單元格作為您的節(jié)的頁眉和/或頁腳。
添加一個額外的單元格,然后將所需的元素放入其中。
將標(biāo)識符設(shè)置為特定的內(nèi)容(在我的情況下為SectionHeader)
實現(xiàn)
tableView:viewForHeaderInSection:
方法或tableView:viewForFooterInSection:
方法用于
[tableView dequeueReusableCellWithIdentifier:]
獲取標(biāo)題實現(xiàn)該
tableView:heightForHeaderInSection:
方法。
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
static NSString *CellIdentifier = @"SectionHeader";
UITableViewCell *headerView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (headerView == nil){
[NSException raise:@"headerView == nil.." format:@"No cells with matching CellIdentifier loaded from your storyboard"];
}
return headerView;
}
編輯:如何更改標(biāo)題標(biāo)題(注釋的問題):
向標(biāo)題單元格添加標(biāo)簽
將標(biāo)簽的標(biāo)簽設(shè)置為特定數(shù)字(例如123)
在您的tableView:viewForHeaderInSection:方法中,通過調(diào)用以下命令獲取標(biāo)簽:
UILabel *label = (UILabel *)[headerView viewWithTag:123];
現(xiàn)在,您可以使用標(biāo)簽設(shè)置新標(biāo)題:
[label setText:@"New Title"];

TA貢獻(xiàn)1872條經(jīng)驗 獲得超4個贊
在iOS 6.0及更高版本中,新dequeueReusableHeaderFooterViewWithIdentifier
API 改變了一切。
我寫了一個指南(在iOS 9上測試過),可以總結(jié)如下:
子類
UITableViewHeaderFooterView
使用子類視圖創(chuàng)建Nib,并添加1個容器視圖,其中在頁眉/頁腳中包含所有其他視圖
注冊筆尖
viewDidLoad
實施
viewForHeaderInSection
并用于dequeueReusableHeaderFooterViewWithIdentifier
獲取頁眉/頁腳
- 3 回答
- 0 關(guān)注
- 621 瀏覽
添加回答
舉報