2013-03-06 57 views
0

我在我的UITableView中有一個customcell。這個自定義單元格有一個標籤和文本框。當用戶填寫數據(4-5欄)並點擊保存按鈕。我想保存他輸入的數據。如何保存數組中的CustomCell數據?

我該怎麼做?

我只有大約5-6場最大。如果你能舉出一些關於我如何完成這件事的例子,那將是非常棒的。

+1

那麼有根據您的具體方案的幾個選項。你想要安全多少數據?只是一些字符串或大量的數據?請編輯您的問題以獲取更具體的信息 – Tobi 2013-03-06 21:32:49

回答

0

單程:將您的數據保存在字典中。

0

你可以讓他將填補數據的數據結構,保存後按鈕被點擊。設置這些值的數據結構 您也可以使用NSMutableDictionary鍵值存儲

示例:假設我們要根據您的意見4 UITextFeilds textFeild1,textFeild2,textFeild3,textFeild14

NSMutableDictionary *dic = [NSMutableDictionary dictionary]; 
[dic addObject: textFeild1.text forValue: @"val1"]; 
[dic addObject: textFeild2.text forValue: @"val2"]; 
[dic addObject: textFeild3.text forValue: @"val3"]; 
[dic addObject: textFeild4.text forValue: @"val4"]; 

//Now you have the values and can retrieve them by: 
NSString *value1 = [dic valueForKey:@"val1"]; 
+0

Hossam - 您可以給我發一段代碼,告訴我它是如何完成的。我是ios編程新手。這將是一個很大的幫助 – simi 2013-03-06 21:57:58

+0

檢查我更新的答案 – 2013-03-06 22:16:33

0

編輯:

在這樣的情況下,蘋果公司建議使用Delegate-Pattern。基本上,你做的是這樣的:

@protocol FirstDataVCDelegate; 

@interface FirstDataVC : UIViewController 

@property (weak, nonatomic) id<FirstDataVCDelegate> delegate; 

//... 

@end 

@protocol FirstDataVCDelegate <NSObject> 

- (void)firstDataVC:(FirstDataVC *)dataVC didCollectData:(NSDictionary *)data; 

@end 


@interface RootVC: UIViewController<FirstDataVCDelegate> 

//... 

@end 

@implementation RootVC 

- (void)firstDataVC:(FirstDataVC *)dataVC didCollectData:(NSDictionary *)data 
{ 
    NSString *name = [data valueForKey:@"name"]; 
    [self.completeData setValue:name forKey:@"name"]; 
} 

@end 

在這種情況下RootVC會是誰想要發送整個數據的VC。 FirstDataVC是用戶輸入數據的VC之一。

每個VC是得到userinput必須提供一個協議,即RootVC工具。

Here是一些更多的代表團。

+0

感謝Tobi。但是,我在這裏使用Customcell並自動構建表格行。 – simi 2013-03-06 21:50:34

+0

對不起,但我不明白你的問題。您是否想要將用戶永久輸入的數據保存到設備中,還是隻想將數據「收集」到變量中? – Tobi 2013-03-06 21:51:56

+0

對不起...我還沒有完成,按下輸入...我沒有與我的字段名稱。它是一個自動生成的表格。使用Customcells – simi 2013-03-06 21:54:32

0

你是不是想將數據傳遞迴的tableview?如果是這樣,您將不得不將信息保存在數據結構中,然後使用定義的協議傳遞它。

+0

我在導航控制器中有4-5個視圖控制器...所以用戶可以前後移動。每個VC有大約5個輸入字段。我想保存用戶在某個數組中輸入的數據,一旦他完成填寫所有數據,然後將其提交給服務器 – simi 2013-03-06 22:03:18

+0

我認爲Tobi回答了您的問題 – sivnatt 2013-03-06 22:28:22

0

嘗試用下面的代碼:

- (UITableViewCell *)tableView: (UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
UITableViewCell *cell = [self.tabTeamDetails dequeueReusableCellWithIdentifier:@"customcellIdentifier"]; 

get UItextfeild and assign tag as below 
    textbox.tag = indexpath.row; //used to refer particular textfiled 
} 

在保存按鈕動作:

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 
//for(i=0;i<noofrowsintableview;i++){ 
NSString *text = (NSString *)[self.view viewWithTag:i]; 
[dic addObject:[NSString stringWithFormat:text] forValue: i]; 
}