2016-06-15 20 views
0

我有一個表格視圖,它將計算cellForRowAtIndexPath中的每個單元格的高度,heightForRowAtIndexPath將使用結果。reloaddata使表視圖的位置出錯

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
      static NSString *CellIdentifier = @"PhotoCell"; 
      UITableViewCell *sectionHeaderView = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (sectionHeaderView == nil) 
    { 
       sectionHeaderView = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

    } 
//setting up the cell 
and also calculate the total height required for this cell and save to _eachcellheight 
} 

它是單元的部分。

- (void)viewWillAppear:(BOOL)animated { 
CGPoint offset = tableView.contentOffset; 
    [tableView reloadData]; 
    [tableView setContentOffset:offset]; 
} 

在視圖中會出現,我需要重新加載表中的數據。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    return _eachcellheight; //option 1 
    return 200; //option 2 
} 

現在的問題是,如果使用一個選項在開始時該表顯示將被罰款,但reloaddata表的偏移後會去怪和滾動去怪也。

但是使用選項2爲所有的每個都返回一個200.f,重新加載數據工作正常,它可以保持與以前相同的位置。 但表格必須具有不同的單元高度。

我該如何解決? 我一直在解決這個問題三天..

回答

0

它是因爲heightForRowAtIndexPath之前調用cellForRowAtIndexPath。移動你的身高計算邏輯heightForRowAtIndexPath而不是cellForRowAtIndexPath,它會正常工作。表格視圖需要知道每個單元格在實際創建單元格之前的高度。

+0

但我需要計算的每個單元格的高度基於cellForRowAtIndexPath中的所有數據。我不能被感動。我可以將高度與可以使用的陣列相提並論嗎?它是否可行? –