UILabel在自定义UITableViewCell中的位置

问题描述:

我有两个UILabels自定义UITableViewCell(.h,.m和.xib文件)。我正在更改UILabels的内容并希望相应地更改其职位。当我在代码中改变位置后执行NSLogs时,它似乎正在工作,但UILabel的屏幕框架保持不变。我究竟做错了什么?UILabel在自定义UITableViewCell中的位置

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifierTwo = @"Cell with labels"; 
    CellWithTwoLabels *cell = (CellWithTwoLabels *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierTwo]; 
    if (cell == nil) { 
     NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"CellWithTwoLabels" owner:nil options:nil]; 
     for (UIView *view in views) { 
     if ([view isKindOfClass:[UITableViewCell class]]) { 
      cell = (CellWithTwoLabels *)view; 
     } 
     } 
    } 
    // First UILabel 
    cell.nameOfDataLabel.text = @"Region"; 
    // Second UILabel 
    cell.valueOfDataLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0]; 
    // Content of second UILabel 
    if (self.region) { 
     cell.valueOfDataLabel.text = self.region; 
    } 
    else { 
     cell.valueOfDataLabel.text = @"Placeholder text"; 
    } 
    // Setting the frame of second UILabel 
    CGSize newSize = [cell.valueOfDataLabel.text sizeWithFont:[UIFont fontWithName:[NSString stringWithFormat:@"%@", cell.valueOfDataLabel.font.fontName] size:cell.valueOfDataLabel.font.pointSize]]; 
    // Assign new size 
    CGRect textFrame = cell.valueOfDataLabel.frame; 
    textFrame.size = newSize; 
    textFrame.origin.x = cell.frame.size.width - textFrame.size.width - 20; 
    cell.valueOfDataLabel.frame = textFrame; 
} 
+0

的可能重复的[与的UITableView动态小区的高度] (http://*.com/questions/3163412/uitableview-with-dynamic-cell-heights) – 2013-05-01 11:12:30

+1

我试过你的代码,它运作良好,你确定你有你的网点连接正确或如果有任何其他重置标签位置的代码? – 2013-05-01 11:43:53

+0

好的,解决方案是勾选单元的.xib文件中的“使用Autolayout”选项......这解决了我的问题。 – user1165156 2013-05-02 10:30:16

你要通过的高度为使细胞与文本一起在

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

并且还用于标签集numberOfLines = 0