TableView数据传递错误

时间:2015-04-07 13:39:34

标签: ios xcode tableview

我跟随一本教科书而且它有点过时,所以我最终将我的代码与它混合在了一起。 它是一个基本的应用程序,用城市(伦敦,旧金山,悉尼和马德里)填充表格视图,点击它,它应该打开一个详细视图,应该显示城市作为其标题和城市的图像与描述不知何故,即使我点击它显示的任何单元格,伦敦"伦敦的图片和描述。 这是我的代码

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(详情视图)

#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

2 个答案:

答案 0 :(得分:0)

您不会在代码中显示如何在CityController上调用此方法

- (id) initWithIndexPath:(NSIndexPath *)indexPath

因此indexPath.row中的CityController始终为零,我假设这是与伦敦相关的城市中的第一个条目

您需要在此方法中执行某些操作,以便为CityController

正确设置indexPath
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

您使用的是故事板吗?

答案 1 :(得分:0)

您的代码必须是,

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CityController *cityVC = [[CityController alloc] initWithIndexPath:indexPath];

    [[self view] addSubview:[cityVC view]];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}