2015-06-05 123 views
-1

我是新来的ios。 我有一个ViewController类,它扫描条形码并将结果保存到NSMutableArray中。然后我想在TableView中显示数组的内容,所以需要在另一个类中使用该数组。在类之间传递NSMutableArray

CODE:

APPDELEGATE.H

@property (nonatomic, strong) ViewController *objViewCont; 

APPDELEATE.M

@synthesize objViewCont; 

VIEWCONTROLLER.H

@property (nonatomic, retain) NSMutableArray *dataSource; 

VIEWCONTROLLER.M

@synthesize dataSource; 
... 
NSString data = //result of scanning 
[dataSource addObject:testData]; 

TABLEVIEWCONTROLLER.M

#import "ViewController.h" 
#import "AppDelegate.h" 

.... 

- (void)viewDidLoad { 
[super viewDidLoad]; 


// Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"]; 
self.navigationItem.rightBarButtonItem = self.editButtonItem; 

AppDelegate *objAppDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; 

// how to populate table with dataSource ???? 
} 

现在,使用objAppDelegate.objViewCont.dataSource我可以访问阵列。问题是:我不知道如何使用dataSource填充表格!

+0

如果在类之间传递数据导致您遇到此类问题,您可能需要如何实施适当的_model-layer_。 (同时,它是关于同一事物的第1,000个帖子,无论如何只有在这个站点上。) – holex

+0

[View Controller之间传递数据]的可能重复(http://stackoverflow.com/questions/5210535/passing-data-between -view控制器) – bgfriend0

回答

0

如果您的问题是如何在uitableview中显示该数组的内容,则需要将该表连接到您的viewcontroller的数据源委托和uitableview委托。

然后,您可以使用委托方法根据数组大小指定每个部分中的行数,然后使用另一个委托来指定在该表视图的每个单元格中显示的内容。

你可以在网上找到很多有趣的教程,快速的谷歌搜索。这就是我如何开始

下面是一个例子:http://www.appcoda.com/ios-programming-tutorial-create-a-simple-table-view-app/

你肯定会用到很多uitableviews的未来。花时间学习它们如何工作并不会带来什么伤害。

享受编码!