2015-09-05 147 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