2014-02-14 43 views
-1

如何将数据发送到另一个详细视图控制器的表视图?在我的第一张桌子上,我没有得到我的预期输出。现在我需要使用具有属性detailContent的数据填充我的第二个表。这是我的代码。如何使用NSdictionary填充数据?

#import <UIKit/UIKit.h> 

@interface RecipeBookViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 

@property (nonatomic, strong) IBOutlet UITableView *tableView; 

@end`` 

\在执行:

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

@interface RecipeBookViewController() 

@end 

@implementation RecipeBookViewController { 
    NSArray *recipes; 
} 

@synthesize tableView; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Initialize table data 
    recipes = [NSArray arrayWithObjects:@"Veg", @"Non Veg", @"Beverages", nil]; 
} 


- (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]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"RecipeCell"; 

    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]; 
     destViewController.detailContent = [recipes objectAtIndex:indexPath.row]; 
    } 
} 


@end 

\详细视图:

@interface RecipeDetailViewController : UIViewController 

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


@end 

在执行:

#import "RecipeDetailViewController.h" 

@interface RecipeDetailViewController() 

@end 

@implementation RecipeDetailViewController 

@synthesize recipeLabel; 
@synthesize recipeName; 

- (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; 
} 

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

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

@end 
+0

费萨尔,究竟是什么的问题?你能推动viewController,或者你没有看到你的数据在下一个视图的问题? –

+0

我想填充第二个表格先生。如果在第一个表格中点击veg,那么我应该在详细视图表中看到像(chappathi,rotti等)的veg列表。如果不是这样(鸡肉,羊肉等)。我不知道在哪里以及如何申报这一个。你能帮忙吗?PLS作为新鲜 –

+0

所以,现在你看不到下一个的viewController的蔬菜,正确的..它会为我更有帮助?如果答案是肯定的,那么你是否检查你的网点? –

回答

-1

要填充的下一个的tableView,你需要实现的UITableView代表和协议符合部门首长 在RecipeDetailViewController
实现:

在RecipeDetailViewController.h
@interface RecipeDetailViewController() <UItableViewDataSource, UITableViewDelegate> 

@end 

@implementation RecipeDetailViewController 

@synthesize recipeLabel; 
@synthesize recipeName; 

- (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; 




} 

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

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

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"RecipeCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

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

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

@interface RecipeDetailViewController : UIViewController 

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

// new 
@property(strong) NSArray recipe; 

@end

回到RecipeBookViewController.m

in PrepareForSegue 

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

thanx帮助先生。它的工作。现在我需要填充数据像chappathi,rotti,同时点击veg。并点击nonveg这样的其他项目..你能说如何申报这一个? –

+0

先生,我有这个“vegDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@”Rotti“,@”Chappathi“,无]详细视图;”现在我该如何传递这些数据?也在我的celforRowAtIndexPath,我有“cell.textLabel.text = [vegDict objectForKey:vegDict];”请解释如何将vegDIct传递到第二张表 –

+0

先生,我有一个更新的代码为同一个。你能帮我输出吗? –

相关问题