iOS dev
iOS dev

Reputation: 2284

How to increase the cell size with respect to the text size in detailTextLabel in iPhone.


-(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;

}


what to do?

Upvotes: 0

Views: 1598

Answers (2)

Maulik
Maulik

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

Daniel Broad
Daniel Broad

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

Related Questions