2013-07-09 44 views
3

這是我想要在我的應用程序。下面顯示的是在iPhone App Store的兩張截圖:UITableView不自然的混亂隨着細胞高度動態變化

Screenshot 1

Screenshot 2

基本上,我需要一個「更多」,就像它在App Store中使用功能(見「描述「部分在上面的兩個圖像)。我假設這裏的每個部分(說明,新增功能,信息等)是一個表格視圖單元格。裏面的文本是UILabel或UITextField。

這是我迄今爲止嘗試添加此功能:

NSString *initialText = @"Something which is not a complete text and created just as an example to..."; 

NSString *finalText = @"Something which is not a complete text and created just as an example to illustrate my problem here with tableviews and cel heights. bla bla bla"; 

NSInteger isReadMoreTapped = 0; 

我的cellForRowAtIndexPath功能:

// Other cell initialisations 
if(isReadMoreTapped==0) 
    cell.label.text = initialText; 
else 
    cell.label.text = finalText; 

return cell; 

我heightForRowAtIndexPath功能:

// Other cell heights determined dynamically 
if(isReadMoreTapped==0){ 
    cell.label.text = initialText; 
    cellHeight = //Some cell height x which is determined dynamically based on the font, width etc. of the label text 
} 
else{ 
    cell.label.text = finalText; 
    cellHeight = //Some height greater than x determined dynamically 
} 
return cellHeight; 

最後我IBAction爲readMoreTapped方法是當按鈕被竊聽稱爲:

isReadMoreTapped = 1; 

[self.tableView beginUpdates]; 

[self.tableView endUpdates]; 

NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0]; // I need to reload only the third row, so not reloading the entire table but only the required one 
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil]; 
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; 

做這一切後,我就得到了所需的功能。計算該特定單元格的新高度,並將新文本加載到該高度。 但是在TableView上有一個非常不自然的混亂,導致不良的用戶體驗。但應用商店更多按鈕雖然不是這種情況。其案件中沒有不自然的混蛋。 TableView保留在它的位置,只有被更改的單元格的大小隨着文本的平滑顯示而增加。

我如何能實現平滑如在iPhone應用程序商店按鈕來完成?

+0

您是否在重載方法中嘗試了其他行動畫? –

+0

你能描述一下「混蛋」嗎?尺寸是否仍然具有新的價值?在滾動位置,單元格的內容是否是一個混蛋? – jrturton

+0

@ LithuT.V是的,我有。行爲沒有變化 –

回答

4

您的問題可能來自重新加載該行。您想嘗試直接配置單元格屬性。我通常使用專門的方法來配置我的單元格內容,所以我不必重新加載行。

- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if(isReadMoreTapped==0) 
     cell.label.text = initialText; 
    else 
     cell.label.text = finalText; 
    // all other cell configuration goes here 
} 

這種方法是從的cellForRowAtIndexPath方法調用,它會配置電池

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    [self configureCell:cell forRowAtIndexPath:indexPath]; 
    return cell; 
} 

,你會直接調用此方法,以避免重裝:

isReadMoreTapped = 1; 

[self.tableView beginUpdates]; 

[self.tableView endUpdates]; 

NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:2 inSection:0]; 
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:rowToReload]; 
[self configureCell:cell forRowAtIndexPath:rowToReload]; 
+0

讓我試試看,結果 –

+0

那麼它幾乎可以解決我的問題。但我不知道爲什麼它也會改變其他細胞的高度。當我點擊** More **按鈕時,所需單元格的高度增加並且其文本得到更新;與此同時,這個細胞上方的細胞高度(主要是它們的高度降低)有不尋常的行爲。在我的'heightForRowAtIndexPath'中,我通過使用'sizeWithFont:constrainedToSize:lineBreakMode:'方法估計標籤的大小來動態計算單元格的高度。方法 –

+0

終於解決了我的問題!部分與你給的部分和其他SO帖子。將更新與答案ASAP –

0

要麼刪除通話到:

[self.tableView beginUpdates]; 
[self.tableView endUpdates]; 

或更改以確保您的重新加載代碼位於它們之間。

[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; 

你只需要指定一個行動畫如淡入:作爲一個單列重載與你使用的方法處理好了我會刪除它們。

1

請嘗試對您的代碼進行以下更改,我認爲它會解決您的問題。

  1. 無需設置cell.label.textheightForRowAtIndexPath;請刪除它們。

  2. readMoreTapped

    ,更新表是足夠的:

    isReadMoreTapped = 1;

    [self.tableView beginUpdates];

    [self.tableView endUpdates];

0

好的,我終於在Matthias的回答(接受的答案)和我自己的優化的幫助下解決了這個問題。必須做的一件事是創建一個專用的函數,如configureCell: forRowAtIndexPath:來直接配置單元屬性(請參見Mathias的答案)。一切仍與馬蒂亞斯的答案除了一樣:

之前,我是計算每個細胞每次的heightForRowAtIndexPath功能的高度被稱爲無緩存(保存)他們的任何地方,因此,當[self.tableView beginUpdates];[self.tableView endUpdates];被稱爲每個單元高度爲再次計算。相反,您需要做的是將這些單元格高度保存在數組/字典中,以便只要點按按鈕,就可以計算僅更改單元格的高度。對於其他單元格,只需查詢數組/字典並返回保存的單元格高度。這應該解決單元高度更新後tableView滾動的任何問題。如果其他人仍然面臨類似的問題,請評論這篇文章。我很樂意提供幫助