2012-01-12 102 views
3

設置我的細胞是這樣的:如何計算通過cellForRowAtIndexPath中的開關設置的單元格的heightForRowAtIndexPath。

所以,一對夫婦開關,確定細胞,因爲數據不是對象的列表,而是一組信息應在一些不同的顯示在的tableView方法。

-(UITableViewCell *)value1CellForTableView:(UITableView *)tableView { 
    static NSString *CellIdentifierValue1 = @"Value1Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierValue1]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifierValue1]; 
     cell.detailTextLabel.textAlignment = UITextAlignmentLeft; 
     cell.textLabel.textAlignment = UITextAlignmentLeft; 
    } 
    return cell; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = nil; 

    switch (indexPath.section) { 
     case 0: 
      //Coupon 
      switch (indexPath.row) { 
       case 0: 
        //Couponcode 
        cell = [self value1CellForTableView:tableView]; 
        cell.textLabel.text = @"Code"; 
        cell.detailTextLabel.text = presentedCoupon.couponNr; 
        break; 
       case 1: 
        //Coupondescription 
        cell = [self value1CellForTableView:tableView]; 
        cell.detailTextLabel.text = presentedCoupon.couponDescription; 
        cell.detailTextLabel.numberOfLines = 0; 
        cell.textLabel.text [email protected]"Ihr Vorteil"; 
        break; 

      }   
      break; 


     case 1: 
      //Productinfo 
      switch (indexPath.row) { 
       case 0: 
        cell = [self defaultCellForTableView:tableView]; 
        cell.imageView.image = [UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: [presentedCoupon.refProdName stringByAppendingString:@".png"]]]; 
        cell.textLabel.text = presentedCoupon.refProdName; 
        break; 



      } 
      break; 

     case 2: 
      //Shopinfo 
      switch (indexPath.row) { 
       case 0: 
        cell = [self defaultCellForTableView:tableView]; 
        cell.textLabel.text = ((Shop*)presentedCoupon.refShop).name; 
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 

        break;  
      } 
      break;  
    } 

    if (cell == nil) { 
     cell = [self defaultCellForTableView:tableView]; 
     cell.textLabel.text = @"Stanni"; 
    } 
    [cell layoutIfNeeded]; 
    return cell; 
} 

而我計算這樣的高度。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
     NSLog(@"height"); 
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath]; 

    CGFloat height = 24 + [cell.detailTextLabel.text sizeWithFont:cell.detailTextLabel.font constrainedToSize: CGSizeMake(cell.detailTextLabel.frame.size.width, 1000.0f) lineBreakMode:cell.detailTextLabel.lineBreakMode].height; 

    return MAX(height, 44.0f); 

問題:

的問題是,在許多線程所提到的,在我的日誌中還可見,每個單元的高度,(可見或不可見)被要求在初始化表視圖。所以在更大的100多個列表中,還會創建100多個單元 - >在啓動時浪費。

當這樣設置單元格時,還有另一種獲取此信息的可能性嗎?是否真的需要在heightForRowAtIndexPath中再次構建開關結構結構以避免這些調用,並且爲每個單元格獲得正確的高度?

用每個單元格的單個信息來保存一個「數據源列表」會更好嗎?

但是如何處理不同的單元格樣式,自定義單元格。

回答

4

兩種可能性:

  1. 多少細胞你有嗎?如果您的電池數量很少(這裏似乎就是這種情況),您不需要重複使用電池!一般來說,電池重複使用過度使用。 在創建控制器或更新數據並將其放入NSArray時,只需創建單元格。
    然後你可以從tableView:cellForRowAtIndexPath:中返回它們或者測量它們的高度。

  2. 創建一個單獨的方法,返回單元格文本/字體並在兩個委託方法中使用它,而不是直接從單元格中讀取信息。

+1

1.我有非常少的細胞。也許大約10。所以你不會使用UITableView的重用事物,並將viewDidLoad中的單元格放置在額外的數組中?但是,你現在如何計算不同風格細胞的高度? 2.這就像在一個額外的方法中再次構建開關盒,因爲這些數據並不是在列表或者某物中。 有沒有比使用tableview來呈現數據更有效的方法? – Denny1989 2012-01-13 09:39:48

+0

你的問題是你從另一個委託方法調用一個委託方法。如果您事先創建了單元格,則可以在兩個委託方法中使用它們,並且不會創建新對象。 – Sulthan 2012-01-13 10:06:41

+0

「或測量它們的高度」 - 要做到這一點,我首先必須強制單元格佈局 - 即使它們還沒有添加到tableview中!我怎麼能這樣做? – 2013-09-09 08:45:13

5

的方法

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

被稱爲每個單元的方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

所以在第一種方法,尚未創建的細胞之前,你不應該試圖訪問它們。

當你創建一個tableView時,首先要求數據源的行數。然後,對於每一行,都要求數據源的行高,以便tableView知道它的內容的總高度,然後最後要求數據源顯示單元格(實際視圖)。

在你的情況下,我會再次構建開關盒結構。關於「數據源列表」,我從來沒有做過,所以也許這是一個更好的解決方案。

0

正如我所見 - 您只有兩種細胞類型。 所以,你可能對每種類型細胞的@property(請看看下面的例子):

static NSString * const kCellIdentifier = @"kCellIdentifier"; 

@interface ... 
@property (nonatomic, retain) UITableViewCell *cell; 
@end 

@implementation 
@synthesize cell = cell_; 

... 

- (UITableViewCell *)cell { 
    if (!cell_) { 
     cell_ = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kCellIdentifier]; 
    } 
    return cell_; 
} 

- (UITableViewCell *)cellForTableView:(UITableView *)tableView { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier]; 
    if (!cell) { 
     //put it to autorelease pool to avoid EXC_BAD_ACCESS 
     cell = [[self.cell retain] autorelease]; 
     self.cell = nil; 
    } 
    return cell; 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = self.cell; 
    CGFloat height = 24 + [@"text" sizeWithFont:cell.detailTextLabel.font constrainedToSize: (CGSize){cell.detailTextLabel.frame.size.width, CGFLOAT_MAX} lineBreakMode:cell.detailTextLabel.lineBreakMode].height; 
    return MAX(height, 44.0f); 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [self cellForTableView:tableView]; 
    cell.detailTextLabel = @"text"; 
    return cell; 
} 

所以細胞將只有一次在開始時被初始化。

2

如果你有一個字符串中對象的數組,並使用標準的表格單元格,然後嘗試這個的iOS 7兼容的魔法:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString* text = [self.objects objectAtIndex:indexPath.row]; 
    NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:text attributes: 
              @{ NSFontAttributeName: [UIFont systemFontOfSize:18]}]; 

    //its not possible to get the cell label width since this method is called before cellForRow so best we can do 
    //is get the table width and subtract the default extra space on either side of the label. 
    CGSize constraintSize = CGSizeMake(tableView.frame.size.width - 30, MAXFLOAT); 

    CGRect rect = [attributedString boundingRectWithSize:constraintSize options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading) context:nil]; 

    //Add back in the extra padding above and below label on table cell. 
    rect.size.height = rect.size.height + 23; 

    //if height is smaller than a normal row set it to the normal cell height, otherwise return the bigger dynamic height. 
    return (rect.size.height < 44 ? 44 : rect.size.height); 
} 
+0

非常感謝! – Carpetfizz 2014-07-10 06:02:50

相關問題