Hi All I'm building a chat function on my app.
In each of my custom table cells I have a UITextView which contains the users comments.
I am trying to Adjust the size of the UITextView depending on the content and then also adjust the height of the cell.
The first problem I have run into is when I try and resize the UITextView with sizeToFit. for some reason it makes the width of the UITextView very sometimes very narrow and often doesn't work at all.
Here is the code I have so far:
- (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;
}
Then for the height of the 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;
}
Can someone just point me in the right direction.