Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
stuart mann
99 PointsPassing data from TableViewCell to DetailViewController
Hi, I have been following the Blog Reader App tutorial which I then with the help of this forum customised so that it would read the URL of my own website.
I'd now like to get some help with passing the blog post to a DetailViewController when a user taps on the cell. The tutorial doesn't really help because it explains a UIWebview and not a DetailViewController and the web is really confusing because the answers are specific to users issues and I can't relate it to my own.
Here is the code that populates the table view:
NSURL *blogURL = [NSURL URLWithString:@"http://theforeground.co.uk/feed/json"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
self.blogPosts = [NSMutableArray array];
NSArray *blogPostsArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
for (NSDictionary *bpdictionary in blogPostsArray) {
BlogPost * blogPost = [BlogPost blogPostWithTitle:[bpdictionary
objectForKey:@"title"]];
blogPost.excerpt = [bpdictionary objectForKey:@"excerpt"];
blogPost.thumbnail = [bpdictionary objectForKey:@"thumbnail"];
blogPost.date = [bpdictionary objectForKey:@"date"];
[self.blogPosts addObject:blogPost];
}
I have a detailviewcontroller.h that look like this:
#import <UIKit/UIKit.h>
#import "BlogPost.h"
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet NSString *postTitle;
@property (weak, nonatomic) IBOutlet UIImageView *postImage;
@property (nonatomic, strong) NSMutableArray *blogPosts;
@end
and a segue from the tableviewcell to the detailviewcontroller called "showdetailview" with a UIView for the blog post image and a UILabel for the blog post title. (I will add the blog post text later once I can't get everything hooked up)
this is my prepareforsegue code, which doesn't have any errors, only warnings which makes me think i am close but not quite there and this is the reason the app fails when I tap to go to the detail view.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowDetailView"])
{
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
[[segue destinationViewController] setPostTitle:blogPost];
}
This is the code for the tableviewcell:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
if ( [blogPost.thumbnail isKindOfClass:[NSString class]]) {
NSData *imageData = [NSData dataWithContentsOfURL:blogPost.thumbnailURL];
UIImage *image = [UIImage imageWithData:imageData];
cell.imageView.image = image;
}
cell.textLabel.text = blogPost.title;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@",blogPost.excerpt,blogPost.date];
return cell;
}
Please can you guys in this great forum help me out. thanks
5 Answers
Thomas Nilsen
14,957 PointsFirst of all, do this: http://imgur.com/F7hpMUq - Then run the code again.
It will help you locate your error.
stuart mann
99 PointsThank you, I did add an exception breakpoint and the app fails in the main when I tap to go to the detail view controller.
stuart mann
99 Pointsthank you, I have made those changes and now the detailviewcontroller is pushed in to view when a cell is tapped. However, the title or image of the cell doesn't display.
Is it a case of writing some code in viewDidLoad to say that the title is one particular IBoutlet and the Image etc are the others?
Thank you for your help this far, I'm slowly getting my head around it.
Amit Bijlani
Treehouse Guest TeacherDo you mean displaying the image and title in your detail view controller? Then you need to have an image view and label. It seems like you need a better understanding of Objective-C and basics of iOS dev. I hope you are going through the track which guides you step by step: iOS Development.
stuart mann
99 Pointsshould i be concerned that there is a warning that says 'unused variable *detailviewcontroller'?
Amit Bijlani
Treehouse Guest TeacherIn your prepareForSegue you create the local variable detailviewcontroller but you don't use it. I would recommend using that directly.
stuart mann
99 PointsCan anyone offer any more help? I've added the breakpoint so i know it fails in the main but I don't know how to fix that. Thanks
Amit Bijlani
Treehouse Guest TeacherThere are several problems with your code:
- You are trying to set a
BlogPostto a string.[[segue destinationViewController] setPostTitle:blogPost];Your post title is a string andblogPostis a of typeBlogPost. At the very least you need to use thetitleproperty of yourblogPost.
[[segue destinationViewController] setPostTitle:blogPost.title];
- Next, you have declared the
postTitleproperty as weak. It should be astrongbecause you want it to live through the life-cycle of you detail view controller.