2015-08-26 41 views
2

我想在iPhone應用程序消失之前在表格視圖中淡化單元格。當它即將出現時,我使用有沒有像iOS的UITableViewDelegate tableViewWillEndDisplayingCell方法?

- (void) tableView: (UITableView *) tableView 
    willDisplayCell: (UITableViewCell *) cell 
forRowAtIndexPath: (NSIndexPath *) indexPath 

將0.3秒的動畫從零變爲1。

我想要一樣的,但從1到零,當它即將消失。是否有類似於上述方法的方法,或者我該怎麼做?

我不想使用標準iOS UITableViewRowAnimationFade,因爲我不想在重新加載表格時進行任何其他轉換。

編輯: 我正在重新加載表視圖的內容。假設我有一個按鈕,我點擊它並重新載入我的表格中的數據。那是當我想淡出現有的單元格並淡入新的單元格時。

我正在使用的控制器是一個基本的UIViewController實施UITableViewDataSourceUITableViewDelegateUIScrollViewDelegate協議。

+0

你可以嘗試viewDidDisappear –

+0

@ T_77這是一個'UIViewController'方法,不是任何可以用於單個單元格(UIView')的方法。 – rmaddy

+0

作者提到了uitableview,但不是uitableviewcontroller。所以我認爲作者在uiviewcontroller中有tableview –

回答

2

我找到了解決辦法。

在致電[self.tableView reloadData]之前,我將所有可見單元格設置爲淡出狀態。我得到的所有可見的單元格,並通過

for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) 
{ 
    // here I animate cells to fade out 
} 

(感謝@AlexKoren用於顯示我遍歷可見單元格的方式)在它們之間迭代。

因爲我在使用[self.tableView reloadData]而沒有任何額外的iOS內置動畫,所以這些單元格正在消失。所以現在他們第一衰落,然後我的新細胞與

- (void) tableView: (UITableView *) tableView 
    willDisplayCell: (UITableViewCell *) cell 
forRowAtIndexPath: (NSIndexPath *) indexPath 

重要定義動畫衰落:我重裝表視圖動畫結束之後。如果我在遍歷單元格之後調用[self.tableView reloadData],它將在動畫顯示之前重新加載表格。因爲我知道需要多長時間才能完成的動畫我用

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void) 
       { 
        [self.tableView reloadData]; 
       }); 

delay之前計算。

0

你試過這個嗎?

- (void)tableView:(UITableView *)tableView 
didEndDisplayingCell:(UITableViewCell *)cell 
forRowAtIndexPath:(NSIndexPath *)indexPath 

我認爲這是你想要的。

+2

這將是完全從UITableView –

1

這是一個黑客位,但應該工作:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    //loop through visible indexPaths 
    for (NSIndexPath *indexPath in [self.tableView indexPathsForVisibleRows]) { 
     //get each cell 
     UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 
     //get its location 
     CGRect rectInTableView = [self.tableView rectForRowAtIndexPath:indexPath]; 
     //set its alpha based on whether or not its going off the screen at the top. 
     cell.alpha = 1 + rectInTableView.origin.y/rectInTableView.size.height; 
    } 

} 
+0

後這將會調用這將工作,但我希望所有可見的單元格淡出時tableview內容重新加載。這隻會在他們即將因滾動而消失時褪色。 – eiKatte

相關問題