0

我尝试创建一个自定义的 uitableviewcell,我用 IB 创建了我的自定义单元,我放置了我的单元的类,连接 IB,然后我以这种方式使用单元:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];

if (cell == nil) {
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = [topLevelObjects objectAtIndex:0];
}

if (indexPath.row == 0) {
    [cell.username setText:@"Load more"];
    [cell.testo setText:@""];
}
else {

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];

    PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
    PFUser *user = [obj objectForKey:@"daUtente"];



    float height = [self getStringHeight:[obj objectForKey:@"TestoMessaggio"]
                                 andFont:[UIFont italicSystemFontOfSize:13]];

    NSLog(@"Height: %f",height);

    [cell.username setFrame:CGRectMake(10, 5, 300, 20)];
    [cell.username setText:[user objectForKey:@"username"]];

    [cell.testo setFrame:CGRectMake(10, 25, 300, height)];
    [cell.testo setText:[obj objectForKey:@"TestoMessaggio"]];
}


return cell;

}

问题是 textview 高度错误,值高度正确但 textview 的框架更小,然后如果我向上/向下滚动 textview 的框架会更改为 IB 的大小。

4

1 回答 1

0

我希望它可以帮助你

 NSString *someText = [NSString stringWithFormat:@"%@",[obj objectForKey:@"TestoMessaggio"]];
 CGSize constraintSize;
 constraintSize.width = 300.0f;
constraintSize.height =100000; 
CGSize stringSize =[someText sizeWithFont: [UIFont boldSystemFontOfSize: 17] constrainedToSize: constraintSize lineBreakMode: UILineBreakModeWordWrap];
    CGRect rect ;
      if (stringSize.height>37) {
           rect = CGRectMake(10, 150, 210, 20+stringSize.height);
      }
    else
     {
       rect = CGRectMake(10, 150, 210, 50);
      }
    yourtextview.frame=rect;
于 2013-07-22T13:58:18.277 回答