【发布时间】:2014-04-18 16:48:33
【问题描述】:
大家好,我正在我的应用上构建聊天功能。
在我的每个自定义表格单元格中,我都有一个 UITextView,其中包含用户 cmets。
我正在尝试根据内容调整 UITextView 的大小,然后还调整单元格的高度。
我遇到的第一个问题是当我尝试使用 sizeToFit 调整 UITextView 的大小时。由于某种原因,它使 UITextView 的宽度有时非常窄并且通常根本不起作用。
这是我目前的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ClubChatCell";
ClubChatCell *cell = (ClubChatCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[ClubChatCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
ClubDetails *club = nil;
club = [_clubs objectAtIndex:indexPath.row];
[cell.commentText setScrollEnabled:YES];
cell.commentText.text = club.comment;
[cell.commentText sizeToFit];
[cell.commentText setScrollEnabled:NO];
cell.fullNameLabel.text = club.creator;
cell.profilePic.image = club.creatorProfilePic;
cell.profilePic.layer.cornerRadius = cell.profilePic.frame.size.height /2;
cell.profilePic.layer.masksToBounds = YES;
cell.profilePic.layer.borderWidth = 0;
cell.timeLabel.text = club.commentDate;
return cell;
}
那么对于TableViewCell的高度:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
ClubDetails *club = nil;
club = [_clubs objectAtIndex:indexPath.row];
NSString *cellText = club.comment;
UIFont *cellFont = [UIFont fontWithName:@"Helvetica" size:14.0];
CGSize constraintSize = CGSizeMake(225.0f, MAXFLOAT);
CGSize labelSize = [cellText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 80;
}
谁能指出我正确的方向。
【问题讨论】:
标签: ios objective-c uitableview