2014-04-14 45 views
1

這是我的故事:Probleme具有高度的UITableViewCell

我有一個問題,我IUTableViewCell的大小。當我添加幾個單元格時,單元格會自動調整大小。

任何答案可以理解的:)

我的代碼來調整:

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

    CustomCell *cell = (CustomCell*) [self tableView:tableView 
         cellForRowAtIndexPath:indexPath]; 
    CGSize size; 

    // SIZE HEIGHT TEXT 
     size = [cell.color.text sizeWithAttributes: 
       @{NSFontAttributeName: 
         [UIFont systemFontOfSize:12.0f]}]; 

     // SIZE HEIGHT FOR CELL 
     CGRect frame = [cell frame]; 
     frame.size.height += size.height; 
     [cell setFrame:frame]; 

     // SIZE HEIGHT IMG 
     CGRect frame = [cell.img frame]; 
     frame.size.height = 69; 
     frame.size.width = 69; 
     [cell.img setFrame:frame]; 

    if (indexPath.row == 0) [self setHeightTableView:0]; 
    _tableHeightConstraint.constant += cell.frame.size.height; 

    return cell.frame.size.height; 
} 

有一些截圖:

我第一次添加單元格一切細

enter image description here

同爲第二單元,一切都很好

enter image description here

還有問題就來了 enter image description here

+2

很確定這是重複的;如果我記得,在tableView:heightForRowAtIndexPathInCellTable:內部調用'[self tableView:tableView cellForRowAtIndexPath:indexPath]'是不好的,因爲iOS在獲取單元格之前需要高度。 – RobP

回答

1

你叫CustomCell

*cell = (CustomCell*) [self tableView:tableView 
         cellForRowAtIndexPath:indexPath]; 

得到一個細胞,這是不對的,因爲沒有尚未創建的細胞(所以其無)。在單元格創建之前調用tableView:heightForCell:atIndexPath:。最好的解決方案是有一個模塊可以保存單元格所需的高度或進行類似的計算