0

I've implemented this tutorial into my project but the segue just won't work! I have tried everything but the cell just stays highlighted when you click it. I have tried adding an identifier to the push - no difference. My tab bar is added programmatically in my appdelegate - could that be it? ALSO the tableview refuses to fit on the screen properly, it overlaps over the status bar (tried pretty much everything for that too).

tableviewcontroller.h

#import <UIKit/UIKit.h>

@interface TableViewController : UITableViewController 


@end

my tableviewcontroller.m

#import "TableViewController.h"
#import "ViewController.h"
#import "AFNetworking.h"

@interface TableViewController ()

@property (strong, nonatomic) NSArray *googlePlacesArrayFromAFNetworking;

@end

@implementation TableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self makeRestuarantRequests];
 self.edgesForExtendedLayout = UIRectEdgeNone;

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)makeRestuarantRequests
{
NSURL *url = [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=restuarants+in+greenwich&sensor=false&key=AIzaSyD-kWneJACswSQfMFQ7sxRPhAEZpHHgvnw"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
//AFNetworking asynchronous url request
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    self.googlePlacesArrayFromAFNetworking = [responseObject objectForKey:@"results"];

    NSLog(@"The Array: %@",self.googlePlacesArrayFromAFNetworking);

    [self.tableView reloadData];


} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"Request Failed: %@, %@", error, error.userInfo);

}];

[operation start];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

// Return the number of rows in the section.
return [self.googlePlacesArrayFromAFNetworking count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if(cell == nil)
{
    cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]autorelease];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}

NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

cell.textLabel.text = [tempDictionary objectForKey:@"name"];

if([tempDictionary objectForKey:@"rating"] != NULL)
{
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Rating: %@ of 5",[tempDictionary   objectForKey:@"rating"]];
}
else
{
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Not Rated"];
}

return cell;
}


#pragma mark - Prepare For Segue

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
ViewController *detailViewController = (ViewController *)segue.destinationViewController;
detailViewController.restaurantDetail = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

}


@end

my appdelegate file (tab bar stuff)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
// Override point for customization after application launch.

//Add tab bar
UITabBarController *tbc = [[UITabBarController alloc]init];

//Specify viewcontrollers for each tab
MapViewController *mvc = [[MapViewController alloc]init];
TableViewController *evc = [[TableViewController alloc]init];
AboutViewController *avc = [[AboutViewController alloc]init];

//Label for tabs
[mvc.tabBarItem setTitle:@"Map"];
[evc.tabBarItem setTitle:@"Eat"];
[avc.tabBarItem setTitle:@"About"];

[mvc.tabBarItem setImage:[UIImage imageNamed:@"103-map.png"]];
[evc.tabBarItem setImage:[UIImage imageNamed:@"125-food.png"]];
[avc.tabBarItem setImage:[UIImage imageNamed:@"28-star.png"]];

[tbc setViewControllers:[NSArray arrayWithObjects:mvc, evc, avc, nil]];


//Add the view controller's view to the window and display.
[self.window setRootViewController:tbc]; //tab bars
[self.window makeKeyAndVisible];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController.edgesForExtendedLayout = UIRectEdgeNone;

return YES;
}

Here is the current output

Here is the debug log: Here is the debug log

Breakpoint error when you click on a table cell Breakpoint error when you click on a table cell

user1336868
  • 109
  • 9

2 Answers2

0

Transition betweent viewcontroller, you should have a navigation controller.

//Specify viewcontrollers for each tab
MapViewController *mvc = [[MapViewController alloc]init];
TableViewController *evc = [[TableViewController alloc]init];
AboutViewController *avc = [[AboutViewController alloc]init];

UINavigationController* mvcNavi = [[UINavigationController alloc] initWithRootViewController: mvc];
UINavigationController* evcNavi = [[UINavigationController alloc] initWithRootViewController: evc];
UINavigationController* avcNavi = [[UINavigationController alloc] initWithRootViewController: avc];

[tbc setViewControllers:[NSArray arrayWithObjects: mvcNavi, evcNavi, avcNavi, nil]];

And you should implement in your tableview the method :

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //Alloc/init detailView
    UIViewController *detailView = [UIViewController alloc]init];
    detailView.restaurantDetail = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

    [self.navigationController pushViewController:detailView animated:YES];
  }

p/s: When you create view programatically, you dont change your view by segue, so remove prepareForSegue

HoanNguyen
  • 405
  • 2
  • 12
  • It says 'restaurantDetail' isn't a property of UIViewController :/ I changed it to VIewController just to see what would happen (that's the detailview page) but the program stops and the breakpoint highlights the line below it: `detailView.restaurantDetail = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];` – user1336868 Mar 27 '14 at 01:26
  • Yep, UIViewController is kind of your detailView. I dont know exactly name your detailView. restaurantDetail is NSDictionary ? – HoanNguyen Mar 27 '14 at 01:30
  • Yes it is, this is how it looks in ViewController.m `self.restuarantNameLabel.text = [self.restaurantDetail objectForKey:@"name"]; [self.restuarantImageView setImageWithURL:[NSURL URLWithString:[self.restaurantDetail objectForKey:@"icon"]]]; self.restuarantAddressLabel.text = [self.restaurantDetail objectForKey:@"formatted_address"];` – user1336868 Mar 27 '14 at 01:37
  • Show me info about error when continue run the program. I think you shoud set breakpoint to debug in didSelect function – HoanNguyen Mar 27 '14 at 01:46
  • added the info to my original post – user1336868 Mar 27 '14 at 12:44
0

If you use segue maybe this code can help:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Assume self.view is the table view
    NSIndexPath *path = [self.tableView indexPathForSelectedRow];
    DetailObject *detail = [self detailForIndexPath:path];
    [segue.destinationViewController setDetail:detail];
}

From here: Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?

Community
  • 1
  • 1
jd.
  • 84
  • 6