自定义UITableViewCell内的UILabel位置

时间:2013-05-01 11:06:43

标签: ios uitableview uilabel

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

- (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;
}

1 个答案:

答案 0 :(得分:0)

您必须传递单元格的高度以及

中的文本
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

并为label = 0设置numberOfLines。

相关问题