表视图单元格 - UITableView - 无法在详细视图中显示多行

时间:2015-09-05 09:21:59

标签: ios objective-c arrays uitableview

我正在使用Tableview显示recipes Array中的名称,效果很好,并在recipes Array的表格视图中显示所有名称。当我点击其中一个食谱时,它会在详细视图中显示名称。

我所困扰的是,如何在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

1 个答案:

答案 0 :(得分: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并在那里显示其他信息。