2014-03-27 71 views
0

我已經在我的項目中實現了this tutorial,但是segue不起作用!我已經嘗試過所有的東西,但是單擊時單元格保持突出顯示。我曾嘗試在推送中添加標識符 - 沒有區別。我的標籤欄以編程方式添加到我的appdelegate中 - 可以這樣嗎?此外tableview拒絕適合在屏幕上,它重疊狀態欄(嘗試幾乎所有的東西)。TableViewController不會延續到詳細視圖

tableviewcontroller.h

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController 


@end 

我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 

我的appdelegate文件(標籤欄的東西)

- (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

當你在一個表格單元格 Breakpoint error when you click on a table cell

+0

我更新了我的回答 – HoanNguyen

回答

0

過渡點擊betweent視圖控制器,你應該有一個導航控制器斷點錯誤。

//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]]; 

而且你應該在你的tableview實現方法:

-(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:當你編程方式創建視圖,您不改變由SEGUE您的看法,所以刪除prepareForSegue

+0

它說'restaurantDetail'不是UIViewController的屬性:/我將它改爲VIewController只是爲了看看會發生什麼(這是detailview頁面),但程序停止並且斷點突出顯示行在它下面:'detailView.restaurantDetail = [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];' – user1336868

+0

是的,UIViewController是一種你的detailView。我不知道你的detailView的確切名稱。 restaurantDetail是NSDictionary? – HoanNguyen

+0

是的,這是它在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

0

如果您使用segue也許這個代碼可以幫助:

- (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]; 
} 

從這裏:Use didSelectRowAtIndexPath or prepareForSegue method for UITableView?

+0

不,我沒有,我會在這裏放什麼? – user1336868