Reputation: 2284
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
cell.detailTextLabel.numberOfLines = 0;
cell.detailTextLabel.lineBreakMode = UILineBreakModeWordWrap;
}
cell.textLabel.text = timelabel.text;
cell.detailTextLabel.text = commentTextView.text;
}
//int x = cell.commentTextView.frame.size.height;
}
cell.detailTextLabel.textAlignment = UITextAlignmentLeft;
cell.textLabel.textAlignment = UITextAlignmentLeft;
return cell;
}
I need to expand my cell size with respect to the length of comments entered in a text view :'commentTextView' displayed in detailTextLabel.
But the difference in the textLable (timelable text), it does not expand along with detailTextLabel
what to do?
Upvotes: 0
Views: 1598
Reputation: 19418
You can do it by using - (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath
delegae method of teble view , like : -
- (CGFloat)tableView:(UITableView *)t heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize lblSize ;
NSString *lblString ;
lblString = [[[dataSourceArray objectAtIndex:indexPath.row] valueForKey:@"aKey"];
lblSize = [self getLebalSize:CGSizeMake(180, 9999) aLebalText:lblString aFont:[UIFont systemFontOfSize:15]];
return lblSize.height + 10 ;
}
//getting labelsize
- (CGSize) getLebalSize : (CGSize) maxSize aLebalText : (NSString *) lebalText aFont : (UIFont *) font
{
CGSize expectedLabelSize = [lebalText sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeTailTruncation];
return expectedLabelSize;
}
Upvotes: 1
Reputation: 2522
This is always tricky to get right, for the height you need to do implement something like this.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *myLabel = "Hello World";
CGSize labelSize = [[myLabel sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(160,MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height;
}
Upvotes: 1