2013-04-11 61 views
3

假設我子類UITableViewCell,並且我有一些視圖(例如UILabel)之內。動畫和更改UITableViewCell視圖內的視圖

已被渲染。 現在我想改變/製作該視圖的動畫。

|      |   |      | 
|-----------------------|   |-----------------------| 
|  label alpha=1.0 | animate |  label alpha=0.5 | 
|-----------------------| ===> |-----------------------| 
|  label alpha=1.0 |   |  label alpha=0.5 | 
|-----------------------|   |-----------------------| 
|      |   |      | 

所以cellForRowAtIndexPathwillDisplayCelldidEndDisplayingCell不是很好的選擇,現在,我想。

我創建了一個方法animateTest子類UITableViewCell這改變了標籤的樣式內部 - 例如顏色或執行一些動畫。

現在在呈現表格的視圖控制器中,我嘗試在tableview子視圖上執行快速啓動。 由於枚舉,我抓到了所有子類UITableViewCell實例,並使用performSelector觸發了animateTest方法。 但沒有任何變化。即使做[tableview reloadData]

如何在表格呈現後爲表格單元格中的某些元素添加動畫? (使用[UIView animateWithDuration.........])?

在此先感謝。

+1

你應該精確一些你的問題:你問如何訪問一個單元格?哪裏可以打電話* animateWithDuration *?我不太確定你的問題。 – rdurand 2013-04-11 12:37:54

+0

對不起,我設法成功訪問了一個單元格(感謝枚舉),但沒有任何更改... - 所以第二個:在哪裏調用animateWithDuration? – Heps 2013-04-11 12:50:40

+0

這取決於什麼觸發動畫。它是一個按鈕,單元格上的一個點,等等? – rdurand 2013-04-11 12:55:04

回答

2

實際上沒有發生任何錯誤的單元初始化。

NSString *cellIdentifier = [NSString stringWithFormat:@"Cell%d", [indexPath section]]; 

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
NSArray *topLevelObjects; 
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil]; 

    for(id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[CustomCell class]]) 
     { 
      cell = (CustomCell *)currentObject; 
      break; 
     } 
    } 

現在我初始化這樣的細胞:

static NSString *reuseIdentifier = @"Cell"; 

CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 

if (cell == nil) { 
    cell = [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0]; 
} 

一切都在使用工作正常:

CustomCell *cell = (CustomCell*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]; 
[UIView animateWithDuration:2 animations:^{ 
    cell.element.alpha = 0; 
}]; 

如果

我以前在下列方式初始化細胞任何人都可以簡單解釋第一種方式出了什麼問題,我將不勝感激。 我懷疑這種初始化以某種方式重置了單元格,所以我的更改不可見,因爲舊單元格(應用了動畫)被丟棄,單元格再次生成。

前段時間我從一些網站複製了片段,所以我習慣了使用它,但顯然這不是一個好的實現。