Increase table height dynamically

I was looking for an increase in the height of a table cell. I got some answers. This is not suitable for my condition.

Happening:

I have table cells with a default height of 150 added. The contents of the table cell (Titile, image, action) are displayed in the table cell.we load the image through "UIImageView + AFNetworking.h" . We have images of different heights.

How to increase / decrease cell height after loading image into cell?

+4
source share
5 answers

To do this, you need to reload your specific cell

// Build the index path NSIndexPath* indexPath1 = [NSIndexPath indexPathForRow:3 inSection:2]; // Add them in an index path array NSArray* indexArray = [NSArray arrayWithObjects:indexPath1, nil]; // Launch reload for the two index path [self.tableView reloadRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationFade]; 

And you need to return the cell height from tableView: heightForRowAtIndexPath: then it will call your tableView: cellForRowAtIndexPath:

PS I assume you are doing lazy loading (loading images after creating the cell)

+1
source

After loading the image into tableview cellForRowAtIndexPath calculate the height of the loaded image. Enter a string in heightForRowAtIndexPath .

Try this code

 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UIImage *Image= [imageAry objectAtIndex:indexPath.row]; NSLog(@"%f",Image.frame.size.height); return size.height + 10; } 
0
source

just set the height of the cell using the method heightForRowAtIndexPath: delegate, as shown below ...

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { UIImage *Tempimage= [yourArray objectAtIndex:indexPath.row]; UIImageView *imgTemp = [[UIImageView alloc]initWithImage:Tempimage]; return imgTemp.frame.size.height; } 
0
source

I do not use UIImageView+AFNetworking.h , but the idea of ​​calculating the line height is to calculate the image height after loading some where

hope this method helps you

 - (void)setImageWithURLRequest:(NSURLRequest *)urlRequest placeholderImage:(UIImage *)placeholderImage success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure; 

and after loading the image in success calculate the height of the image image.size.height and add this to the array for the table

 NSMutableDictionary *record = [array objectAtIndex:index]; [record setObject:[NSString stringWithFormat:@"%f",image.size.height] forKey:@"imageheight"]; [array replaceObjectAtIndex:index withObject:record]; [tableView reloadData]; 

and set a line height similar to this

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSMutableDictionary *record = [array objectAtIndex:index]; if ( [record objectForkey:@"imageheight"] ) { return [[record objectForkey:@"imageheight"] floatValue] + yourmargin); // yourmargin may be 150 or what ever be } else { return 150; } } 
0
source

After loading the image, follow these lines:

[self.tblStoryCardView beginUpdates]; [self.tblStoryCardView endUpdates];

And load the image height into the line height method.

I uploaded a dummy project on github. you can check this: https://github.com/govindprasadguptacool/Dynamic-Table-Cell-Height

0
source

All Articles