私は最終的にそれが機能するようになりました.私のプロジェクトのコード:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"shopmessage";
UITableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel* content = (UILabel*)[cell viewWithTag:11];
UILabel* date = (UILabel*)[cell viewWithTag:12];
NSDictionary* msg = [ _msglist objectAtIndex:indexPath.row];
NSString* type = [[msg objectForKey:@"msgtype"] intValue] == 0 ? @"我:":@"店家:";
[content setText:[NSString stringWithFormat:@"%@%@",type,[msg objectForKey:@"sendmsg"]]];
[date setText:[msg objectForKey:@"sendtime"]];
CGSize contentsize = [content.text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:UILineBreakModeWordWrap];
// content.frame.size = contentsize;
content.frame = CGRectMake(content.frame.origin.x, content.frame.origin.y, contentsize.width, contentsize.height);
[date setText:[msg objectForKey:@"sendtime"]];
// date.frame.origin = CGPointMake(date.frame.origin.x, content.frame.origin.y + content.frame.size.height + 10);
date.frame = CGRectMake(date.frame.origin.x, content.frame.origin.y + content.frame.size.height, date.frame.size.width, date.frame.size.height);
return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSDictionary* msg = [ _msglist objectAtIndex:indexPath.row];
NSString* text =[msg objectForKey:@"sendmsg"];
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:17] constrainedToSize:CGSizeMake(280, 1000) lineBreakMode:UILineBreakModeCharacterWrap];
return size.height + 30;
}
参考:Apple サポートコミュニティ