UITableView headerViewForSection 返回 (空值)

我有UITableView2 個(gè)部分。每個(gè)都有它自己的 headerView 。
我已經(jīng)創(chuàng)建了一個(gè)自定義headerView通過(guò) -viewForHeaderInSection:方法。
后來(lái),我計(jì)劃有點(diǎn)修改它,所以我需要使用 viewForHeader方法,但不能訪問(wèn)headerView和它有 subViews。

作為一個(gè)簡(jiǎn)單的例子,我在試著 NSLog viewForHeader 中的對(duì)象-didSelectRowAtIndexPath:但我得到(null)的結(jié)果。

示例代碼:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 75;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{    
    UIView *myHeader = [[UIView alloc] init];
    switch (section) {
        case 0:
            [myHeader setBackgroundColor:[UIColor greenColor]];
            break;
        case 1:
            [myHeader setBackgroundColor:[UIColor redColor]];
            break;
        default:
            break;
    }

    UILabel *myLabel = [[UILabel alloc] init];
    [myLabel setFrame:CGRectMake(10, 0, 100, 30)];
    [myLabel setTag:101];
    [myLabel setBackgroundColor:[UIColor clearColor]];
    [myLabel setText:[NSString stringWithFormat:@"Section: %d",section]];

    [myHeader addSubview:myLabel];    
    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewHeaderFooterView *testView = [self.tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",testView);  //displays (null)
}
解決方法 1:
方法 1:
好在一些試驗(yàn)和錯(cuò)誤后我終于解決我自己的困境。我做了 `headerView` 只會(huì)一樣一個(gè)單元格。

為一個(gè)單元格,我們會(huì)采取 `UITableViewCell `和使用`dequeueReusableCellWithIdentifier`
雖然......
為的頁(yè)眉頁(yè)腳,我們將采取 `UITableViewHeaderFooterView `,并使用 `dequeueReusableHeaderFooterViewWithIdentifier `方法。
剩下的就很多相同的概念作為一個(gè)單元格。

先決條件:
- 設(shè)置的頁(yè)眉的高度到 40
- 設(shè)置的節(jié)數(shù)為 2 或更多
- 每節(jié)要至少 1 個(gè)設(shè)置的行數(shù)
`iOS6 + ( UITableViewHeaderFooterView `與 `iOS5` 和下面不會(huì)工作)
第一種方法代碼:

創(chuàng)建和使用默認(rèn)的 UITableViewHeaderFooterView內(nèi) -viewForHeaderInSection:方法:

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

    UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if(!myHeader) {
        myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
    }

    UIButton *btnUp = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnUp setTag:101];
    [btnUp setTitle:@"-" forState:UIControlStateNormal];
    [btnUp setFrame:CGRectMake(tableView.frame.size.width - 35, 5, 30, 30)];
    [myHeader addSubview:btnUp];

    [myHeader.textLabel setText:[NSString stringWithFormat:@"Section: %d",section]];

    [myHeader setFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)];
    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewHeaderFooterView *theHeaderView = [tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",theHeaderView); // -- great! ... not (null) anymore

    UIButton *theButton = (UIButton *)[theHeaderView viewWithTag:101];
    [theButton setTitle:@"+" forState:UIControlStateNormal];
}
第二種方法:

使用自定義 UITableViewHeaderFooterView子類:

  • 1.創(chuàng)建 UITableViewHeaderFooterView子類并將它命名為CustomHeaderView
  • 2.創(chuàng)建了一個(gè)視圖界面 nib 文件的類
  • 3.Xib,在選定視圖& 在中它是身份檢查器,指定的自定義類CustomHeaderView
  • 4.作的性質(zhì)、 合成和連接他們的 xib
@property (strong, nonatomic) IBOutlet UILabel *lblSomething;
@property (strong, nonatomic) IBOutlet UIButton *btnSomething;

修改-viewForHeaderInSection: & -didSelectRowAtIndexPath: 作為:

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

    CustomHeaderView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];
    if(!myHeader) {
    //    [tableView registerClass:[CustomHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifier];
        myHeader = [[[NSBundle mainBundle] loadNibNamed:@"CustomHeaderView"
                                                  owner:self
                                                options:nil] objectAtIndex:0];
    }

    [myHeader.btnSomething setTitle:@"-" forState:UIControlStateNormal];
    [myHeader.lblSomething setText:[NSString stringWithFormat:@"Section: %d",section]];

    return myHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomHeaderView *theHeaderView = (CustomHeaderView*)[tableView headerViewForSection:indexPath.section];
    NSLog(@"%@",theHeaderView);

    [theHeaderView.lblSomething setAlpha:theHeaderView.lblSomething.alpha-0.1];
    [theHeaderView.btnSomething setTitle:@"+" forState:UIControlStateNormal];
}

PS: 與問(wèn)題 UITableViewHeaderFooterView 是它是 iOS6 + 如果,任何原因,您的標(biāo)頭/必須是 UIView ,然后看看下一個(gè)方法

方法 二代碼:

使用一個(gè)簡(jiǎn)單的 UIView

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *vwHeader = [[UIView alloc] init];
    [vwHeader setTag:200 + section]; //[1] first method

    switch (section) {
        case 0:
            [vwHeader setBackgroundColor:[UIColor greenColor]];
            break;
        case 1:
            [vwHeader setBackgroundColor:[UIColor redColor]];
            break;
        default:
            break;
    }

    UILabel *lblTitle = [[UILabel alloc] init];
    [lblTitle setFrame:CGRectMake(10, 0, 100, 30)];
    [lblTitle setTag:100 + section]; //[2] alternative method
    [lblTitle setBackgroundColor:[UIColor clearColor]];
    [lblTitle setText:[NSString stringWithFormat:@"Section: %d",section]];

    [vwHeader addSubview:lblTitle];
    return vwHeader;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIView *vwTest = [self.tableView viewWithTag:200 + indexPath.section]; //[1]
    NSLog(@"[1] : %@",vwTest);

    //or

    UILabel *lblTest = (UILabel *)[self.tableView viewWithTag:100 + indexPath.section]; //[2]
    NSLog(@"%@",lblTest.text);
    UIView *vwTestForSuperview = lblTest.superview;
    NSLog(@"[2] : %@",vwTestForSuperview);
}

文章原地址

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 標(biāo)簽: 知識(shí)管理 1.你的知識(shí)需要管理 這本書是一本知識(shí)管理的書籍,整本書以對(duì)知識(shí)的 學(xué)習(xí)——保存——分享——使用...
    烽煙亂閱讀 852評(píng)論 4 10
  • 昨天早上,讀《親愛(ài)的安德烈》。又讀到那篇“我為什么要叫你用功讀書”,曾在各界媒體的筆下無(wú)數(shù)次的“巧遇”它。我看這題...
    螢火蟲8848閱讀 322評(píng)論 0 4
  • 周六,本來(lái)應(yīng)該愜意的一天,可是一頓中午飯把我吃進(jìn)了苦海。肚子難受了一下午。下午時(shí)嗓子又突然不舒服,滿滿的不開心。充...
    與城閱讀 219評(píng)論 0 0

友情鏈接更多精彩內(nèi)容