2011-07-01 53 views
0

我已創建此表視圖節標題。它基本上是一個UIView容器,其中包裝了將在該部分標題上的所有元素。動畫表格視圖節標題

此容器視圖是由

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

返回,一切工作正常。

現在我希望標題在表格出現時淡入淡出。因此,我最初爲容器聲明alpha = 0,然後在viewDidAppear:上執行此操作(嗯,此表位於顯示的視圖控制器中)。

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    [UIView animateWithDuration:1.0 
     animations:^{ 
      [self.tableHeader setAlpha:1.0f]; 
    }]; 

} 

沒有任何反應,頭部仍然不可見。

我已嘗試添加:

[self.tableView beginUpdates]; //and 
[self.tableView beginUpdates]; 

之前所提到的動畫後,都沒有成功。

在我看來,表頭不更新,並繼續不可見。

+0

在沒有動畫的情況下是否會更改alpha? –

+0

根本沒有變化。 – SpaceDog

+1

也許發佈最初定義alpha = 0.0的地方。這可能會覆蓋動畫。 – PengOne

回答

5

首先,把NSLog兩個viewDidAppeartableView:viewForHeaderInSection:

你會看到viewDidAppear執行第一次的tableView有一個異步加載,你不知道什麼時候就會調用viewForHeaderInSection

一個解決方法如下:

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

    _tableHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)]; 
    _tableHeader.backgroundColor = [UIColor redColor]; 
    _tableHeader.alpha = 0; 

    [UIView animateWithDuration:1.0 
       animations:^{ 
        [_tableHeader setAlpha:1.0f]; 
       }]; 

    return _tableHeader; 

} 

只要打電話時,表將返回viewHeader動畫。

+0

就是這樣。完善。我認爲在該方法返回之前視圖是不可見的,但它是。謝謝! – SpaceDog