2014-09-25 55 views
0

所以我看了this answer。但我仍然沒有得到正確的iOS7返回的高度。我有一個表格視圖,我在自動佈局中放置了一個相當複雜的單元格。每當我嘗試覆蓋heightForRowAtIndex(使其與iOS7兼容)時,我會得到似乎無法正確計算的行高。當我不覆蓋heightForRowAtIndex一切都很好,但它不適用於iOS7。我也嘗試使用xib自動佈局整個事情。我的DipticCell.m中沒有任何邏輯。全部在UITableView子類中。使用UITableViewCells和動態高度自動佈局

-(id)init{ 
    //Implementation details go BELOW 
    self.dataSource = self; 
    self.delegate = self; 
    [self registerNib:[UINib nibWithNibName:@"DipticCell" bundle:nil] forCellReuseIdentifier:@"dipticCell"]; 
    [self registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"]; 
    self.rowHeight = UITableViewAutomaticDimension; 
    offScreenCells = [[NSMutableDictionary alloc] init]; 
    return self; 

} 
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    NSString *reuseIdentifier = @"dipticCell"; 
    if (indexPath.row > 0){ 
     reuseIdentifier = @"cell2"; 
    } 

    UITableViewCell *cell = [offScreenCells objectForKey:reuseIdentifier]; 
    if (!cell) { 
     cell = [self dequeueReusableCellWithIdentifier:reuseIdentifier]; 
     [offScreenCells setObject:cell forKey:reuseIdentifier]; 
    } 


    [cell setNeedsUpdateConstraints]; 
    [cell updateConstraintsIfNeeded]; 


    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds), CGRectGetHeight(cell.bounds)); 


    [cell setNeedsLayout]; 
    [cell layoutIfNeeded]; 

    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height; 

    height += 1.0f; 
    return height; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if (indexPath.row == 0){ 
     DipticCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dipticCell"]; 
     if (cell == nil) { 
      cell = [[DipticCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"dipticCell"]; 
     } 
     return cell; 
    }else{ 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"]; 
     if (cell == nil) { 
      cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell2"]; 
     } 
     cell.textLabel.text = @"one"; 
     return cell; 
    } 

} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return 10; 
} 
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

只是爲了確認一切都在contentView中。

enter image description here

此外,當我重寫heightForRowAtIndexPath我得到一個「無法同時滿足的約束。」警告。但是當我不覆蓋那東西似乎工作(這是有道理的,因爲細胞應該適應內容的高度,現在我試圖覆蓋它的高度爲1.)

這裏是一個鏈接到項目,如果你想運行它。 AutoLayoutCells

+0

鏈接的項目只是一個空的視圖。它沒有任何表格。 – 2014-09-25 17:10:20

+0

固定。我不知道爲什麼它第一次沒有正確推送... – 2014-09-25 17:36:38

+0

如果我在下載它之後直接運行該項目,在iOS 8模擬器上,它無法正確顯示。紅色文本單元格被壓縮。是不是應該在iOS 8上看起來不錯?另外,UIScrollView的用途是什麼?現在看起來空了。有時候滾動視圖會在放入單元格時產生問題(因爲UITableView是一個UIScrollView子類),你不能刪除它嗎? – 2014-09-26 10:06:53

回答

0

大部分時間對我來說有效的是確保您有一條從單元格頂部到單元格底部的約束路徑。

因此,如果您的手機作爲三個標籤:label1,label2,label3和全部應顯示在彼此之下。比添加約束:

cell.top to label1.top 
label1.bottom to label2.top 
label2.bottom to label3.top 
label3.bottom to cell.bottom 

那樣你的細胞的高度是由約束定義的。因此,請確保連接所有定義此鏈中單元格高度的子視圖。

+0

我加倍檢查,一切都很好。就像我說的那樣,當我取出heightForRowAtIndexPath位時,它可以工作,但是當我將它添加回來時,會錯誤地估計高度。 – 2014-09-25 18:27:47

相關問題