2016-08-05 37 views
0

的高度,我在heightForRowAtIndexPath-問題雖然發現的UITableViewCell

我的完整方法計算細胞的高度編程是:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID]; 

    if([cell isKindOfClass:[Custom class]]) 
    { 
     return 100; 
    } 
    else 
    { 
     return 20; 
    } 
} 

我的應用程序需要3-5秒顯示的視圖控制器。儘管調用了cellForRowAtIndexPath,我可以在控制檯中顯示日誌。

但我的ViewController未加載。

當剛剛返回使用此代碼的應用程序的高度正常工作:

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

    return 100; 
} 

我不知道這是這一行的問題:

id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID]; 

什麼,我在這個問題上失蹤?

+0

看到這個http://stackoverflow.com/questions/18206448/how-can-i-get-the-height-of-a-specific-row-in-my-uitableview和http://stackoverflow.com/questions/13968316/get-uitableviewcells-height-after-setting-height-for-each-uitableviewcell –

+0

@ Anbu.Karthik我需要找到細胞的類型,因爲我有兩種類型的細胞。這是主要問題。每當我使用代碼來查找單元格類型時,我的應用程序開始延遲 – Dalvik

+2

爲什麼不使用屬性isTableViewWithCustomCells或isTableViewWithOtherCustomCells?它應該更快。我認爲可以在cellForRowAtIndexPath和出隊的東西之前調用'heightForRowAtIndexPath:'。 – Larme

回答

0

您在cellForRowAtIndexPath:方法中有一些邏輯確定該行應該是哪種類型的單元格。將該邏輯添加到heightForRowAtIndexPath:方法中以確定每行的高度應該是多少。

0

您此行是錯誤的

id cell = [self.tableView dequeueReusableCellWithIdentifier:kCellID]; 

它會迫使你的重用。

使用

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    id cell=[tableView cellForRowAtIndexPath:indexPath]; 

    if([cell isKindOfClass:[CustomCellClass class]]) 
    { 
     return 100; 
    } 
    else 
    { 
     return 20; 
    } 
}