我在我的UITableView中有一個customcell。這個自定義單元格有一個標籤和文本框。當用戶填寫數據(4-5欄)並點擊保存按鈕。我想保存他輸入的數據。如何保存數組中的CustomCell數據?
我該怎麼做?
我只有大約5-6場最大。如果你能舉出一些關於我如何完成這件事的例子,那將是非常棒的。
我在我的UITableView中有一個customcell。這個自定義單元格有一個標籤和文本框。當用戶填寫數據(4-5欄)並點擊保存按鈕。我想保存他輸入的數據。如何保存數組中的CustomCell數據?
我該怎麼做?
我只有大約5-6場最大。如果你能舉出一些關於我如何完成這件事的例子,那將是非常棒的。
單程:將您的數據保存在字典中。
你可以讓他將填補數據的數據結構,保存後按鈕被點擊。設置這些值的數據結構 您也可以使用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"];
Hossam - 您可以給我發一段代碼,告訴我它是如何完成的。我是ios編程新手。這將是一個很大的幫助 – simi 2013-03-06 21:57:58
檢查我更新的答案 – 2013-03-06 22:16:33
編輯:
在這樣的情況下,蘋果公司建議使用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是一些更多的代表團。
嘗試用下面的代碼:
- (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];
}
那麼有根據您的具體方案的幾個選項。你想要安全多少數據?只是一些字符串或大量的數據?請編輯您的問題以獲取更具體的信息 – Tobi 2013-03-06 21:32:49