定制cellcustom uitableveiwcell与IB

时间:2013-07-22 13:03:19

标签: uitableview uitextview

我尝试创建一个自定义的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的大小。

1 个答案:

答案 0 :(得分: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;