簡體   English   中英

將單元格中的UITextField文本保存到NSUserDefaults中?

[英]Saving UITextField text in cell into NSUserDefaults?

我已經看到每個帖子都接近這個問題,但仍然沒有找到有用的東西。 我在每個單元格中都有textFields,用作表單供用戶填寫。 除了滾動之外,單元格中的所有內容都能正常工作,當單元格滾出屏幕時,textFields中的輸入會消失。 我知道這是因為出隊。 但應該有一種方法來保存輸入的數據,以便在滾動或退出應用程序時不會消失。 我還希望能夠獲取此信息並通過電子郵件將其作為PDF或文檔發送。 實現這一目標的最佳方法是什么? 下面的代碼是我如何生成我的細胞等的一個例子。

.h文件

@interface MasterViewController : UITableViewController <UITextFieldDelegate, UITextFieldDelegate, UITableViewDataSource, UINavigationBarDelegate>{

    NSString* name_;
    UITextField* nameFieldTextField;

}

// Creates a textfield with the specified text and placeholder text
-(UITextField*) makeTextField: (NSString*)text
              placeholder: (NSString*)placeholder;

// Handles UIControlEventEditingDidEndOnExit
- (IBAction)textFieldFinished:(id)sender;

@property (nonatomic,copy) NSString* name;

.m文件

@synthesize name = name_;

- (void)viewDidLoad{

[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];

self.name = @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    // Make cell unselectable and set font.
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:13];

    if (indexPath.section == 0) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {
            cell.textLabel.text = @"Name" ;
            tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
            nameFieldTextField.tag = 1;
            [cell addSubview:nameFieldTextField];
            break ;
    }
    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
 }
 return cell;
}


// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {

        //Section 1.
    if ( textField == nameFieldTextField ) {
    self.name = textField.text ;
}
}

- (void)viewWillAppear:(BOOL)animated{

    NSString *nameCellString = [[NSUserDefaults standardUserDefaults] stringForKey:@"nameCellString"];
nameFieldTextField.text = nameCellString;

}

- (void)viewWillDisappear:(BOOL)animated{

    NSString *nameCellString = self.name;

    [[NSUserDefaults standardUserDefaults] setObject:nameCellString forKey:@"nameCellString"];

}

這里實際上有兩個問題,它們都在你的cellForRowAtIndexPath:實現中。

  • 您正在將文本字段放入單元格,即使此單元格已重復使用且已有文本字段。 因此,您實際上是在文本字段上堆疊文本字段,覆蓋以前存在的文本字段。

  • 如果該行的文本字段中已存在文本(索引路徑),則不會將文本放回文本字段中。

換句話說,細胞是(正如你所說的那樣)重復使用,因此你需要考慮這個事實。 您必須查看傳入單元的狀態,並相應地重新配置單元。

首先,我建議你考慮在故事板中創建一個自定義單元格,並抓住它。 它比編碼一個容易得多,我認為這是未來。 也就是說,考慮使用NSArrays填充tableViews,而不是將字符串硬編碼到cellForRowAtIndexPath方法中。 我冒昧地給你一個例子。

以下內容基於您的代碼,應該是復制/粘貼解決方案。 仔細看看,看看它是如何運作的。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *titlesArray = @[@"Name", @"Birthday", @"Favorite Food"];

    UITableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"%i%i", indexPath.section, indexPath.row]];

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

    // Make cell unselectable and set font.
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont fontWithName:@"ArialMT" size:13];

    // Populate label from array
    cell.textLabel.text = titlesArray[indexPath.row];
    if (indexPath.section == 0) {

    UITextField* tf = nil;
    switch ( indexPath.row ) {
        case 0: {

            tf = nameFieldTextField = [self makeTextField:self.name placeholder:@"John Appleseed"];
            nameFieldTextField.tag = 1;
            [cell addSubview:nameFieldTextField];
            break ;
    }
    // Textfield dimensions
    tf.frame = CGRectMake(120, 12, 170, 30);

    // Workaround to dismiss keyboard when Done/Return is tapped
    [tf addTarget:self action:@selector(textFieldFinished:) forControlEvents:UIControlEventEditingDidEndOnExit];
 }
    // Set the reuse identifier to a unique string, based on placement in table
    // This ensures that the textField will retain its text
    cell.reuseIdentifier = [NSString stringWithFormat:@"%i%i", indexPath.section, indexPath.row];

}
 return cell;
}


// Textfield value changed, store the new value.
- (void)textFieldDidEndEditing:(UITextField *)textField {

        //Section 1.

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    switch (textField.tag) {
        case 1:
            [defaults setObject:textField.text forKey:@"nameCellString"];
            self.name = textField.text;
            break;
        default:
            break;
    }

    [defaults synchronize];
}

編輯:更改以容納更多單元格。

我認為解決問題的最簡單方法是為您的單元格創建一個新類(從UITableViewCell繼承)並添加新的屬性,如customerTextField (UITextField) 在構造函數中添加新的文本字段,但使用CGRectZero 在方法layoutSubviews中,您將為CGRect字段分配CGRect 一般來說,這種方法將使您的UIViewController更清晰(您將減少文本字段狀態的檢查次數)。

您應該為tableDataSourceArray使用UIDictionary數組,如:

step1) 

NSArray *tableDataSourceArray = [[NSArray alloc]init];
NSMutableDictionary *cellData = [[NSMutableDictionary alloc]init];
[cellData setValue:@"" forKey:@"Name"];
...//so on
[tableDataSourceArray addObject:cellData];
cellData = nil;

repeat step1 as number of records you have.

現在在cellForRowAtIndexPath

 nameFieldTextField.tag = indexPath.row; //To store index of dataSourceArray
    nameFieldTextField.text = [[tableDataSourceArray objectAtIndex:indexPath.row] valueForKey:@"Name"];

最后在textFieldDidEndEditing

NSMutableDictionary *cellDataDic = tableDataSourceArray objectAtIndex:textField.tag];

[cellDataDic setValue:textField.text forKey:@“Name”];

希望它會對你有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM