2015-06-05 125 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的未來。花時間學習它們如何工作並不會帶來什麼傷害。

享受編碼!