I have a UITableView which should show news. I did set up 2 custom cells -> one has the news without an image, and the other one has the news with image. So the cells need different heights. In storyboard I created those 2 Cells and they each have a custom height (280 with image, 130 without).
For testing purposes I want the first and the third cell to be a news cell + image. So thats my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary * news = [postArray objectAtIndex:indexPath.row]; //PostArray is the array with the news
NSDictionary *authorData = [news valueForKey:@"author"];
NSString *postTitle = [self decodedString:[news valueForKey:@"title"]];
//Decode = change html chars
NSString *postExcerpt = [self decodedString:[news valueForKey:@"excerpt"]];
NSString *postComments = [news valueForKey:@"comment_count"];
//NSString *postID = [news valueForKey:@"id"];
NSString *authorFirstName = [self decodedString:[authorData valueForKey:@"first_name"]];
NSString *authorLastName = [self decodedString:[authorData valueForKey:@"last_name"]];
NSString *authorNickName = [self decodedString:[authorData valueForKey:@"nickname"]];
if (indexPath.row == 0 || indexPath.row == 2){
NewsCellImage *cell = (NewsCellImage *)[tableView dequeueReusableCellWithIdentifier:@"NewsCellImage"];
if (cell == nil) {
cell = [[NewsCellImage alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCellImage"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:@"%i",indexPath.row];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
}
//Set Image
NSDictionary *imageData = [news valueForKey:@"thumbnail_images"];
NSDictionary *imageDataFull = [imageData valueForKey:@"large"];
NSString *postImage = [imageDataFull valueForKey:@"url"];
[cell.postImage setImageWithURL:[NSURL URLWithString:postImage]
placeholderImage:[UIImage imageNamed:@"menu"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if (error){
NSLog(@"Error bei Bild laden: %@",postTitle);
}
} usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
return cell;
} else {
NewsCell *cell = (NewsCell *)[tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
if (cell == nil) {
cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsCell"];
}
// Set up the cell
cell.postTitle.text = postTitle;
cell.postExcerpt.text = postExcerpt;
cell.postCommentsCount.text = [NSString stringWithFormat:@"%@",postComments];
//Check if names are empty
if ([authorFirstName length] == 0 && [authorLastName length] == 0){
cell.postMeta.text = [NSString stringWithFormat:@"%@ / %@",authorNickName,[news valueForKey:@"date"]];
} else {
cell.postMeta.text = [NSString stringWithFormat:@"%@ %@ / %@",authorFirstName,authorLastName,[news valueForKey:@"date"]];
}
return cell;
}
}
The code does work in general and thats what I can see after starting the app: http://cl.ly/image/320K1y081T3Z
But when I scroll down to the next cell with Image something goes wrong: http://cl.ly/image/2M2O1X3R2T13
So something is wrong with my code or I need to add something - but I dont know which way to take.
Maybe it's something with
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
But I really dont know. I appreciate help. Ty
How does your:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{ }
look then?
I think you need something like:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexpath{
if (indexpath.row == 0 || indexpath.row == 2) {
return 280;
}
return 130;
}