0

hello I have UITable view there by default cell height 44.0 and now we want to increase this according to cell content like as image enter image description here

I am using this but this is not working .....

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   int rowHeight =0.0f;
//  NSString *stringToSize = [shared.combine_address objectAtIndex:indexPath.row];
 CGSize size = [ [shared.combine_address objectAtIndex:indexPath.row] sizeWithFont:[UIFont    systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(743, 44) lineBreakMode:NSLineBreakByWordWrapping];
rowHeight = self.table.rowHeight+size.height;
return rowHeight;
}


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *MyIdentifier = @"MyIdentifier";

//   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

SWTableViewCell *cell2 = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];


if(tableView==table)
{
    //static NSString *cellIdentifier = @"Cell";

    if (cell2 == nil) {

        cell2 = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier:MyIdentifier
                                   containingTableView:table // Used for row height and selection
                                    leftUtilityButtons:nil
                                   rightUtilityButtons:[self rightButtons]];
        cell2.delegate = self;
        cell2.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    NSLog(@"CELL 1 %@",cell2.textLabel.text);
    cell2.textLabel.lineBreakMode=NSLineBreakByWordWrapping;
    cell2.textLabel.numberOfLines=0;
    cell2.textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15.0f];
    cell2.detailTextLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell2.detailTextLabel.numberOfLines = 0;
    [cell2.textLabel sizeToFit];

}
return cell2;

 }
Rahul Sharma
  • 940
  • 2
  • 12
  • 31

3 Answers3

0

Either the width is wrong or the shared.combineAddress is not having a value.

try giving width as 320

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   int rowHeight =0.0f;
   NSString *stringToSize = [shared.combine_address objectAtIndex:indexPath.row];
   CGSize size = [stringToSize sizeWithFont:[UIFont systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(320, 44) lineBreakMode:NSLineBreakByWordWrapping];
   rowHeight = self.table.rowHeight+size.height;
   return rowHeight;
}

And also check if the stringToSize has some value in it.

Edit:

    UILabel *sizeCalculationLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 0)];
    sizeCalculationLabel.numberOfLines = 0;
    [sizeCalculationLabel setFont:[UIFont systemFontOfSize:13.0f]];
    sizeCalculationLabel.text = stringToSize;
    [sizeCalculationLabel sizeToFit];
    return sizeCalculationLabel.frame.size.height;
Selvin
  • 12,333
  • 17
  • 59
  • 80
  • shared.combine_address have values and I have already do this but not work. – Rahul Sharma Apr 05 '14 at 06:54
  • @Selvin I don't think this is right approach as for every cell it will initialise a label. Its a process to create an Object which is not necessary. Also it will increase your memory level. – Kapil Choubisa Apr 05 '14 at 07:16
0

I think you are using iPad. If your UITableView's width is more than 743 than you can change following.

change constrainedToSize:CGSizeMake(743, 44) value to constrainedToSize:CGSizeMake(743, 999).

Make sure you pass the width max as your tableView's width. So if your UITableView's height is 320 (standard for iPhone) than use constrainedToSize:CGSizeMake(320, 999).

As you are passing constrained size with 44 height it says that max height should be 44. So you have to pass a maximum possible size you want to display may be 100, 200 or any number. I am setting it to 999.

Also sizeWithFont is deprecated in iOS 7. So better to use

- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context

in iOS 7.

Here is the example:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
   int rowHeight =0.0f;
   CGSize size;
   if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        NSDictionary *stringAttributes = [NSDictionary dictionaryWithObject:[UIFont    systemFontOfSize:13.0f] forKey: NSFontAttributeName]; 
        size = [[shared.combine_address objectAtIndex:indexPath.row] boundingRectWithSize:CGSizeMake(743, 999) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin  attributes:stringAttributes context:nil].size;

   }
   else {
       size = [ [shared.combine_address objectAtIndex:indexPath.row] sizeWithFont:[UIFont    systemFontOfSize:13.0f] constrainedToSize:CGSizeMake(743, 999) lineBreakMode:NSLineBreakByWordWrapping];
   }
   rowHeight = self.table.rowHeight+size.height;
   return rowHeight;
}
mohitdream4u
  • 166
  • 10
Kapil Choubisa
  • 5,152
  • 9
  • 65
  • 100
0

table view cell use dynamic height UITableViewAutomaticDimension

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

 return UITableViewAutomaticDimension;
 }

 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{

 return 44.0f;

 }
Pratik Lad
  • 464
  • 4
  • 9