0

テキスト入力用に2つの異なるテキストフィールドを持つ UITableView があります。テーブルビュー全体をループしてコア データに保存するか、結果を配列にコピーしてからコア データに保存する方法はありますか。テーブルビューセルを作成するコードは次のとおりです

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Add Terms to New Set";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...
questionTextField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 250, 30)];
questionTextField.adjustsFontSizeToFitWidth = YES;
questionTextField.textColor = [UIColor blackColor];
//questionTextField.placeholder = @"Put Question Here";
questionTextField.keyboardType = UIKeyboardTypeDefault;
questionTextField.returnKeyType = UIReturnKeyDone;
[questionTextField.layer setBorderColor: [[UIColor grayColor] CGColor]];
[questionTextField.layer setBorderWidth: 0.5];
[questionTextField.layer setCornerRadius:8.0f];
[questionTextField.layer setMasksToBounds:YES];

[questionTextField setEnabled: YES];

answerTextField = [[UITextField alloc] initWithFrame:CGRectMake(400, 10, 250, 30)];
answerTextField.adjustsFontSizeToFitWidth = YES;
answerTextField.textColor = [UIColor blackColor];
//answerTextField.placeholder = @"Put Answer Here";
answerTextField.keyboardType = UIKeyboardTypeDefault;
answerTextField.returnKeyType = UIReturnKeyDone;

[answerTextField.layer setBorderColor: [[UIColor grayColor] CGColor]];
[answerTextField.layer setBorderWidth: 0.5];
[answerTextField.layer setCornerRadius:8.0f];
[answerTextField.layer setMasksToBounds:YES];


[answerTextField setEnabled: YES];

[cell addSubview:questionTextField];
[cell addSubview:answerTextField];
return cell;

}

テーブルビューをループして、質問テキストと回答テキストを配列に追加するか、コア データ モーダルに直接保存するにはどうすればよいですか?

ありがとう!

4

1 に答える 1

1

データを保存するには、別の戦略を採用する必要があります。ビュー(つまりセル)に依存して保持するのではなく、ユーザーが入力するとすぐにバッキング配列に配置する必要があります。

表示する行の総数はわかりませんが、スクロールがある場合は、キャッシュのために行よりもセルが少なくなり、セルが戻ってきたときに入力された情報が失われることがほぼ保証されます。また。

同じ理由で、 nilを返さcellForRowAtIndexPath:ない限り、セルにフィールドを追加しないでくださいdequeueReusableCellWithIdentifier:...キャッシュされたフィールドにはすでにそれらのフィールドがあります。

于 2012-07-03T00:41:35.010 に答える