2015-09-05 152 views
1

显示我使用泰伯维显示名称从recipes Array其中工程从recipes Array表视图很大,并显示所有名称多行。当我点击其中一个配方时,它会在详细视图中显示名称。表视图细胞 - 的UITableView - 不能在详细视图

我被卡住的是如何显示其他字段在detailed view。我有一个数组(array name: ingredients),其中包含具体配方的内容,例如成分。

cell.textLabel.text = [ingredients objectAtIndex:indexPath.row]; 

试图RecipeDetailViewController.h

#import <UIKit/UIKit.h> 

@interface RecipeDetailViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel *recipeLabel; 
@property (nonatomic, strong) NSString *recipeName; 

@property (strong, nonatomic) IBOutlet UILabel *recipeCountry; 
@property (nonatomic, strong) NSString *recipeCountryName; 

@end 

,但它并没有显示在RecipeBookViewController.m 什么这是我的原代码

#import "RecipeBookViewController.h" 
#import "RecipeDetailViewController.h" 

@interface RecipeBookViewController() 

@end 

@implementation RecipeBookViewController { 
    NSArray *recipes; 
} 

@synthesize tableView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    NSData *allCourseData=[[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]]; 

    NSError *error; 
    NSMutableDictionary *JsonObject = [NSJSONSerialization 
             JSONObjectWithData:allCourseData options:NSJSONReadingMutableContainers 
             error:&error]; 

    NSArray *loans = JsonObject[@"loans"]; 
    // Create a new loans array for store the new items 
    NSMutableArray *newLoans = [NSMutableArray array]; 
    NSMutableArray *arrCountry = [NSMutableArray array]; 


    for (NSDictionary *loan in loans) { 
     NSNumber *ident = loan[@"id"]; 
     NSString *name = loan[@"name"]; 
     NSDictionary *description = loan[@"description"]; 
     NSDictionary *location = loan[@"location"]; 
     NSString *country = location[@"country"]; 
     NSString *status = loan[@"status"]; 

     NSDictionary *geo = location[@"geo"]; 
     NSString *geoprint = geo[@"pairs"]; 

     [newLoans addObject:name]; 
     [arrCountry addObject:country]; 
    } 
    recipes=newLoans; 

} 


- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [recipes count]; 
} 

//Display Name in Detail View 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"RecipeCell"; 

    //Displays name 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [recipes objectAtIndex:indexPath.row]; 

    return cell; 
} 


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) { 
     NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
     RecipeDetailViewController *destViewController = segue.destinationViewController; 
     destViewController.recipeName = [recipes objectAtIndex:indexPath.row]; 
    } 
} 


@end 

RecipeDetailViewController.m

#import "RecipeDetailViewController.h" 

@interface RecipeDetailViewController() 

@end 

@implementation RecipeDetailViewController 

@synthesize recipeLabel; 
@synthesize recipeName; 
@synthesize recipeCountry; 
@synthesize recipeCountryName; 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Set the Label text with the selected recipe 
    recipeLabel.text = recipeName; 
    recipeCountry.text=recipeCountryName; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 

这里有两种观点

enter image description here enter image description here

+0

您需要向我们显示显示详细视图的代码。 – deadbeef

+0

@deadbeef我为RecipeDetailViewController.m添加了代码 – user580950

+1

显示您在哪里导航到RecipeDetailVIewController并显示您的主/成分数组数据。 – Shoaib

回答

2

变化在循环的截图;

for (NSDictionary *loan in loans) { 
     NSNumber *ident = loan[@"id"]; 
     NSString *name = loan[@"name"]; 
     NSDictionary *description = loan[@"description"]; 
     NSDictionary *location = loan[@"location"]; 
     NSString *country = location[@"country"]; 
     NSString *status = loan[@"status"]; 

     NSDictionary *geo = location[@"geo"]; 
     NSString *geoprint = geo[@"pairs"]; 

     [newLoans addObject:@[ 
      @"name":name, 
      @"description":description, 
      @"country ":country 
     ]]; // Same way add all keys 

     //Or 
     /* 
     NSMutableDictionary* dicData = [NSMutableDictionary dictionary]; 
     newLoans[@"name"] = name; 
     newLoans[@"description"] = description; 
     newLoans[@"country"] = country; 
     [newLoans addObject:dicData]; 
     */ 

    } 
    recipes=newLoans; 

检索时发生变化;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    . . . 
    NSDictionary * dict = [recipes objectAtIndex:indexPath.row]; 
    cell.textLabel.text = dict[@"name"]; 

    return cell; 
} 

同样的方法将完整的字典传递给RecipeDetailViewController并在其中显示其他信息。

+0

我得到错误http://i.imgur.com/h27PQu5.png – user580950

+0

想法是在你的数组中有一个字典,而不是一个字符串。我没有机会在xcode中测试代码,所以如果有必要的话,你必须自己纠正语法错误。 BTW更新了这篇文章。 – Shoaib

+0

我的代码已经在使用 cell.textLabel.text = [recipes objectAtIndex:indexPath。行]; 我的问题是如何添加一个更多的数组与国名在它作为 cell.textLabel.text = [countryname objectAtIndex:indexPath.row]; – user580950