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