2013-05-11 59 views
0

我一直試圖找出這個相當長的一段時間IOS泰伯維自定義單元格:動態標籤高度

我有tableview中有兩個標籤,一個低於其他自定義單元格。頂部標籤可以是一行或兩行最大值。底部標籤僅限於一行。

頂部的標籤將永遠在那裏,但底部的標籤將在那裏的一些細胞,我不在那裏的一些其他細胞。

我需要一種方法來弄清楚如何安排這些標籤垂直居中。我嘗試使用cell.label1.numberOfLines = 0;製作頂部標籤動態但是然後label1並不僅限於2行。

我試圖通過計算第一個標籤的高度來垂直居中這些標籤,然後將兩個標籤都置於中心位置。

如果我使用xib將行數限制爲2,那麼即使標籤消耗1行,第一個標籤的高度始終計算爲2行。我總是看到的一個問題是,如果我使用[cell.label1 sizeToFit];,那麼在滾動表格時,標籤總是會改變顯示的文字。所顯示的文字總是用於相應的標籤,但文字有時會中途包裹,有時使用整條線。

這裏是我的代碼以供參考:

CustomCell1 *cell = (CustomCell1 *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
if (cell == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil]; 
    cell = [nib objectAtIndex:0]; 
    cell.label1.lineBreakMode = UILineBreakModeWordWrap; 
    cell.label1.numberOfLines = 0; 
    cell.label1.tag = [indexPath row]; 
} 
cell.label1.text = [[responseArray objectAtIndex:[indexPath row]] text]; 
if([[[responseArray objectAtIndex:[indexPath row]] isDone] isEqualToString:@"N"]) { 
    cell.label2.text = @""; 
} else { 
    cell.label2.text = @"Done"; 
} 

//[cell.label1 sizeToFit]; 

CGSize label1Size = [cell.label1 frame].size; 
CGFloat label1Height = label1Size.height; 
CGRect label1Frame = cell.label1.frame; 
int numLines = (int)(label1Height/cell.label1.font.leading); 

CGSize label2Size = [cell.label2 frame].size; 
CGFloat label2Height = label2Size.height; 
CGRect label2Frame = cell.label2.frame; 

if(numLines > 1) { 

    if([cell.label2.text isEqualToString:@""]) { 
     //if label2 == "" then center label1 
     label1Frame.origin.y = cellHeight/2 - label1Height/2; 
     cell.label1.frame = label1Frame; 
    } else { 
     label1Frame.origin.y = 12; 
     cell.label1.frame = label1Frame; 
     label2Frame.origin.y = 52; 
     cell.label2.frame = label2Frame; 
    } 
} else { 
    if([cell.label2.text isEqualToString:@""]) { 
     CGRect frame = cell.label1.frame; 
     frame.origin.y = cellHeight/2 - label1Height/2; 
     cell.label1.frame = frame; 
    } else { 
     label1Frame.origin.y = cellHeight/2 - label1Height/2; 
     cell.label1.frame = label1Frame; 
     label2Frame.origin.y = cellHeight/2; 
     cell.label2.frame = label2Frame; 
    } 
} 

請幫我IOS大師您的建議。

+0

,您應該使用CustomCell的'layoutSubviews'執行這些操作。在'tableView:heightForRowAtIndexPath:'中查找單元格的高度並執行'layoutSubviews'。 – Anupdas 2013-05-11 19:03:46

+0

如何使用layoutSubviews將它修復。我不想動態改變單元格的高度,而是想要垂直居中單元格中的兩個標籤。我很抱歉,我是IOS的新手。所以請解釋我是否缺少一些東西。 – user1060418 2013-05-11 19:43:30

+0

你可以檢查我的答案。 – Anupdas 2013-05-11 20:11:31

回答

3

將調整大小的計算移動到layoutSubviews會更好。

//CustomCell.m 

- (void)layoutSubviews{ 

CGRect label1Frame = self.label1.frame; 
//Calculate the size of label 1 
CGSize label1Size = [self.label1.text sizeWithFont:[self.label1 font] 
           constrainedToSize:CGSizeMake(label1Frame.size.width,42.0f) 
            lineBreakMode:NSLineBreakByTruncatingTail]; 
label1Frame.size.height = label1Size.height; 

CGRect label2Frame = self.label2.frame; 
//Calculate the size of label 2 
CGSize label2Size = [self.label2.text sizeWithFont:[self.label2 font] 
           constrainedToSize:CGSizeMake(label2Frame.size.width,21.0f) 
            lineBreakMode:NSLineBreakByTruncatingTail]; 
label2Frame.size.height = label2Size.height; 

//Total height of labels 
CGFloat totalHeight = label1Frame.size.height+label2Frame.size.height+5.0f; 

//For centering half of difference of two labels with cell 
label1Frame.origin.y = (self.frame.size.height - totalHeight)/2.0f; 
self.label1.frame = CGRectIntegral(label1Frame); 

label2Frame.origin.y = label1Frame.origin.y+label1Frame.size.height+5.0f; 
self.label2.frame = CGRectIntegral(label2Frame); 

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    CustomCell1 *cell = (CustomCell1 *)[self.tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 
    if (cell == nil) 
    { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell1" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
     cell.label1.lineBreakMode = UILineBreakModeWordWrap; 
     cell.label1.numberOfLines = 2; 
     cell.label1.tag = [indexPath row]; 
    } 
    cell.label1.text = [[responseArray objectAtIndex:[indexPath row]] text]; 
    if([[[responseArray objectAtIndex:[indexPath row]] isDone] isEqualToString:@"N"]) { 
     cell.label2.text = @""; 
    } else { 
     cell.label2.text = @"Done"; 
    } 

    return cell; 

} 
+1

這個修復了我的問題!謝謝。 – user1060418 2013-05-30 09:05:32