Thanks to all. I tried yours solutions and they work but no completely. The problem with the height is almost solved but I'm experiencing an issue with cell visualization because, when they are a little bit longer, they are not shown completely (images neither texts). The screenshots show first and second rows. You can see that until you scroll down a bit the second row is not visible and, in order to see it, you should scroll down until image is only displayed partially.
This is not being so easy as I thought in the beginning.
Thank you for your comments and help.
Talking about code, I have used incmiko's solution but I still have a problem downloading images because I should do it asynchronously and after that Table View must be redrawn to fit image sizes but I'm not sure how to do it.
Custom Cell:
@interface NewsFeedCell : UITableViewCell<UITextViewDelegate>
{
UIImageView *picView;
UITextView *postContent;
float cellHeight;
}
@property (nonatomic, retain) UIImageView *picView;
@property (nonatomic, retain) UITextView *postContent;
@property (nonatomic) float cellHeight;
//-(id)initWithImageUrlString:(NSString *)imgUrl AndText:(NSString *)postText;
-(void)setPostImage:(NSString *)imgUrl;
-(void) setPostContenText:(NSString *)postText;
-(void) setcontentHeight;
-(float) getCellHeight;
@end
Custom cell is iniatilized only with a TextView since some of the future content won't have any image (I know I will have to take care about this while calculating height and Content View size).
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
[self setFrame:CGRectMake(CELL_LEFT_MARGIN, 0, CELL_TOTAL_WIDTH, 50)];
postContent = [[UITextView alloc] initWithFrame:CGRectMake(CELL_LEFT_MARGIN, 0, CELL_TOTAL_WIDTH, 100)];
[self.contentView addSubview:postContent];
}
return self;
}
I'm using AFNetworking to download images:
-(void)setPostImage:(NSString *)imgUrl
{
picView =[[UIImageView alloc] initWithFrame:CGRectMake(0,0, 50, 50)];
[picView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:imgUrl]] placeholderImage:nil success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *pic )
{
//NSLog(@"Loaded successfully: %d", [response statusCode]);
[picView setImage:pic];
[self.contentView addSubview:picView];
CGRect frame = CGRectMake((self.bounds.size.width/2-pic.size.width/2), 0, pic.size.width, pic.size.height);
[picView setFrame:frame];
frame = self.contentView.frame;
frame.size.height = picView.frame.size.height;
self.contentView.frame = frame;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"failed loading: %@", error);
}
];
}
-(void)setPostContenText:(NSString *)postText
{
CGSize stringSize = [self calculateTextHeight:postText];
CGRect frame = postContent.frame;
frame = CGRectMake(CELL_LEFT_MARGIN, picView.frame.origin.y+picView.frame.size.height, CELL_TOTAL_WIDTH, stringSize.height);
postContent.frame = frame;
postContent.autoresizingMask = UIViewAutoresizingFlexibleHeight;
postContent.font = [UIFont systemFontOfSize:POST_CONTENT_FONT_SIZE];
postContent.text= postText;
postContent.textColor=[UIColor blackColor];
[postContent setContentInset:UIEdgeInsetsZero];
[postContent setBackgroundColor:[UIColor whiteColor]];
[postContent setEditable:NO];
frame.size.height = stringSize.height + 5.0f;
}
-(void) setcontentHeight
{
if(self)
{
CGRect frame = self.contentView.frame;
frame = self.contentView.frame;
frame.size.height = postContent.frame.size.height + picView.frame.size.height;
self.contentView.frame = frame;
self.cellHeight = self.contentView.frame.size.height + 10;
NSLog(@"picView height: %f", picView.frame.size.height);
NSLog(@"textview height: %f", postContent.frame.size.height);
}
else
self.cellHeight = 44.0f;
}
And Controller that calls Custom cells:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (self.cellsArray && self.cellsArray.count) {
return self.cellsArray.count;
} else {
return 0;
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.cellsArray[indexPath.row] getCellHeight];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return self.cellsArray[indexPath.row];
}
#pragma mark -
-(void)createCellArray
{
static NSString *simpleTableIdentifier = @"PostCell";
if (!self.cellsArray) {
self.cellsArray = [[NSMutableArray alloc]init];
}
for(NSDictionary *post in posts)
{
NSString *postText = [post objectForKey:@"longDescription"];//[posts objectAtIndex:indexPath.row];
NewsFeedCell *cell= [[NewsFeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
if(cell)
{
[cell setPostImage:[post objectForKey:@"artworkUrl100"]];
[cell setPostContenText:postText];
[cell setcontentHeight];
[cellsArray addObject:cell];
}
}
}