0

我有两个 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;
}
4

1 回答 1

0

您必须将单元格的高度与文本一起传递

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

并为 label = 0 设置 numberOfLines。

于 2013-05-01T13:16:05.323 回答