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
Alex Atwater
2,624 PointsTable Delegate not present in IOS 7 build?
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath wasn't included into my file and was looking forever for it. It's just not there. Your video iOS 6 build, i'm working with iOS7. is that a difference? i manually added that line, and everything complied and worked fine, but when i added the necessary code to open it in Safari, the app wouldn't do it. It would just highlight the tab. Any reason why this is happening and how to fix it Amit Bijlani ?
George S-T
6,624 PointsHaving same problem Amit Bijlani Pasted code:
import "TableViewController.h"
import "BlogPost.h"
@interface TableViewController ()
@end
@implementation TableViewController
(id)initWithStyle:(UITableViewStyle)style { self = [super initWithStyle:style]; if (self) { // Custom initialization } return self; }
-
(void)viewDidLoad { [super viewDidLoad];
NSURL *blogURL = [NSURL URLWithString:@"http://blog.teamtreehouse.com/api/get_recent_summary/"];
NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; NSLog(@"%@", dataDictionary);
self.blogPosts = [NSMutableArray array]; NSArray *blogPostsArray = [dataDictionary objectForKey:@"posts"]; for (NSDictionary *bpDictionary in blogPostsArray) { BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]]; blogPost.author = [bpDictionary objectForKey:@"author"]; blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"]; blogPost.date = [bpDictionary objectForKey:@"date"]; blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]]; [self.blogPosts addObject:blogPost]; }
}
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }
pragma mark - Table view data source
-
(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1; }
-
(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section. return [self.blogPosts count]; }
-
(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; }
else { cell.imageView.image = [UIImage imageNamed:@"Tree"]; }
cell.textLabel.text = blogPost.title; cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@",blogPost.author, [blogPost formattedDate]];
return cell; }
/* // Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the specified item to be editable. return YES; } */
/* // Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) { // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view }
} */
/* // Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath { } */
/* // Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { // Return NO if you do not want the item to be re-orderable. return YES; } */
/*
pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. }
*/
@end
7 Answers
Stone Preston
42,016 PointsGeorge Symes-Thompson I found your error. you wrote tableview instead of tableView
you implemented
- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
when you should have implemented
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
Stone Preston
42,016 PointsJust implement it yourself
Just add
- void)tableView: (UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
To your .m file
Sorry I'm on mobile so I can't format very well. Xcode 5 doesn't add it automatically like it did before, so just add the method yourself.
George S-T
6,624 PointsDoesn't work. It doesn't have any errors but If I put an NSLog in there nothing happens.
Stone Preston
42,016 Pointsit should work. Are your other tableView delegate methods running? are you using a tableViewController or do you just have a tableView inside some other view controller. Also be sure you implement didSelectRow and not didDeselectRow
George S-T
6,624 PointsYep I believe so, here is my not working code:
-
(void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row]; UIApplication *application = [UIApplication sharedApplication]; [application openURL:blogPost.url]; }
Stone Preston
42,016 Pointsand if you put an NSLog inside that method such as
- (void)tableview:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"row selected");
BlogPost *blogPost = [self.blogPosts objectAtIndex:indexPath.row];
UIApplication *application = [UIApplication sharedApplication];
[application openURL:blogPost.url];
}
you dont see anything in the console?
George S-T
6,624 PointsCorrect.
Stone Preston
42,016 Pointsif you can you post your project on github/dropbox ill take a look at it and see whats going on.
George S-T
6,624 PointsDeepak Govindasamy
4,668 PointsAny update on this one ?
Stone Preston
42,016 Pointsjust implement the method yourself.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
Deepak Govindasamy
4,668 PointsThanks. It worked !
Stone Preston
42,016 Pointsglad you got it figured out
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherCan you paste your code?