I'm following a textbook and it's a bit outdated so I ended up mixing my code with it. Its a basic app that populates a table view with Cities (london,san francisco,sydney and madrid) and upon clicking it, It should open up a detail view that should display the city as its title and an image of the city with the description, somehow even if I click any cell it shows me "London" with London's picture and description. Here's my code
ViewController.m
#import "ViewController.h"
#import "City.h"
#import "AppDelegate.h"
#import "CityController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];
citys = appDelegate.cities;
}
#pragma mark - UITableViewDataSource Methods
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
City *theCity = [citys objectAtIndex:indexPath.row];
cell.textLabel.text = theCity.cityName;
return cell;
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [citys count];
}
#pragma mark - UITableViewDelegate Methods
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
CityController.m (the detail View)
#import "CityController.h"
#import "AppDelegate.h"
#import "City.h"
@implementation CityController
- (id) initWithIndexPath:(NSIndexPath *)indexPath{
if((self = [super init])){
index = indexPath;
}
return self;
}
- (void) viewDidLoad{
[super viewDidLoad];
AppDelegate *delegate = [[UIApplication sharedApplication]delegate];
City *thisCity = [delegate.cities objectAtIndex:index.row];
self.title = thisCity.cityName;
labelDescription.text = thisCity.cityDescription;
//descriptionView.editable = NO;
pictureView.image = thisCity.cityImage;
}
@end
City.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface City : NSObject
@property (nonatomic, strong) NSString *cityName;
@property (nonatomic, strong) NSString *cityDescription;
@property (nonatomic, strong) UIImage *cityImage;
@end
You don't show in your code how you are calling this method on your CityController
- (id) initWithIndexPath:(NSIndexPath *)indexPath
Therefore indexPath.row
in CityController
will always be zero which I'm assuming is the first entry in the cities that relates to London
You need to do something in this method to set the indexPath correctly for the CityController
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
Are you using Storyboards?
Your code must be,
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CityController *cityVC = [[CityController alloc] initWithIndexPath:indexPath];
[[self view] addSubview:[cityVC view]];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.