2013-02-28 19 views
1

由於在cellForRowAtIndexPath之前調用了heightForRowAtIndexPath,所以我假設如果我想修改cellForRowAtIndexPath以內的高度,我可以這樣做。看來我不能。我已經通過NSLog進行了測試,如果我更改cell.frame.size.height,更改會正確存儲在那裏,但單元本身不會佔用新的大小(它使用我在heightForRowAtIndexPath中設置的大小)。試圖在cellForRowAtIndexPath中設置單元格的高度,在heightForRowAtIndexPath之後

是否有另一種方法可以調整在cellForRowAtIndexPath之後的某個點調用單元格的高度?如果不是,還有其他解決方法嗎?我需要使用cellForRowAtIndexPath,因爲我正在決定是否將圖像隨機添加到每個單元格中。

+0

heightForRowAtIndexPath是一個委託調用。你的委託是否放在桌子上? – Jbryson 2013-02-28 16:21:27

+0

是的,'heightForRowAtIndexPath'和'cellForRowAtIndexPath'都被正確調用。 – Luke 2013-02-28 16:21:49

回答

2

您應該隨機決定在您的heightForRowAtIndexPath方法中添加圖像代碼。

使用NSMutableDictionary來跟蹤使用圖像的NSIndexPath

+0

這不是一個真正的選擇,因爲我使用核心數據和NSFetchedResultsController在結果生成後過濾結果。這樣做會改變索引路徑,所以事情會變得非常糟糕,不是嗎? – Luke 2013-02-28 16:26:11

+0

在這種情況下,如果您的核心數據對象具有某種唯一的ID(或事件objectID),則可以將其用作NSDictionary的鍵而不是indexPath。 – 2013-02-28 16:29:45

+0

它的確如此,但是如何工作?當使用heightForRowAtIndexPath,我對象的ID添加到一個數組,如果它決定了它會是一個圖像..然後做cellAtRowForIndexPath的時候,我會打好圖像出來自己的ID是否在該數組中發現了什麼? – Luke 2013-02-28 16:30:43

0

您應該隨機決定在cellForRowAtIndexPath之前的某個時間,然後在數據源的數據結構中爲每個indexPath存儲一個標誌,無論是否存在圖像。如果決定是否存在圖像的工作便宜,則可以在heightForRowAtIndexPath中執行此操作。類似這樣的:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    BOOL showingAnImage = arc4random() % 2; 
    // self.imageIndexPaths is some data structure that lets you associate a value to an index path 
    self.imageIndexPaths[indexPath] = @(showingAnImage); 
    if (showingAnImage){ 
     // return height of a cell with an image 
    } else { 
     // return height of a cell without an image 
    } 
} 


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath]; 
    if ([self.imageIndexPaths[indexPath] boolValue]){ 
     // Configure for image 
    } else { 
     // Configre without image 
    } 
    return cell; 
} 

在cellForRowAtIndexPath中更改單元格的大小:將不起作用。

+0

我正在使用Core Data和NSFetchedResultsController在結果生成後過濾結果。這樣做會改變索引路徑,所以事情會變得非常糟糕,不是嗎? – Luke 2013-02-28 16:28:40

+0

看起來像Vinny Coyne擊敗了我! – ehope 2013-02-28 16:28:50

+0

你爲什麼說它會失控?這樣,您將預先確定索引路徑中的單元格是否具有圖像,而不管您在表格視圖中顯示的數據如何排序。 – ehope 2013-02-28 16:29:55

6

UITableViewDelegate heightForRowAtIndexPathUITableView rowHeight是指定單元高度的唯一機制。 tableview本身就是基於這些來調整你的單元格的大小;你不能自己設置你的細胞框架,並期望它工作。

您可以做的最好的事情是能夠在細胞被創建之前預先計算細胞高度。我經常會定義一個+ (CGFloat) cellHeightWithDatum: (id) datum forWidth: (CGFloat) tableWidth方法來調用我的單元類heightForRowAtIndexPath。這裏的數據是驅動單元內容的模型。然後這個方法看着模型並計算出細胞需要的高度。

如果您確實需要更改單元格的高度,一旦細胞被創建,你可以做到這一點通過要求的tableview重新裝入電池,或刷新整個表瓦特/輸出調用reloadData。這一招做的:

[tableView beginUpdates]; 
[tableView endUpdates]; 

要重新加載單個單元格:UITableView reloadRowsAtIndexPaths:withRowAnimation:

0

你需要記住這需要擴大指數的軌道。

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 

    if([self shouldBeExpanded:indexPath.row]) { //method that checks if this cell should be expanded. You'll need to check the array if it contains the row. 
     return kCellHeightExpanded; 
    } 

    return kCellNormalHeight; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

    //set cell to be expanded 
    [self expandCellAt:indexPath.row]; //method that sets which cell is expanded. You'll need to implement this yourself. Use an array to track which row should be expanded 

    //this causes heightForRowAtIndexPath: to be called again 
    [tableView beginUpdates]; 
    [tableView endUpdates]; 
} 
相關問題