0

可変配列が設定されたテーブルがあります。セルはカスタムで、UITextViewがあり、customcellclassに委任されています。この値は、保持したいと思います。テーブルをreloadDataするたびに消去されます。

それを維持する方法について何か考えはありますか?私が持っていた、そして行くことができなかったアイデア:

  • textView値は、テーブルに入力されているのと同じ配列に格納します。どこから?TextViewDidEndEditing?値を渡す方法と、それがどの行であったかを知る方法は?
  • textViewの配列を他の配列とは別に維持します。セルテーブルの通信に関する同じ問題があり、この配列も維持する必要があります。さらに、textViewを.nibから削除する必要があります。...私はこれ以上の知識を思いつきませんでした。

どんなアプローチや助けも大歓迎です。ありがとうございました!

PS:私は多くの検索を行いましたが、ほとんどがフォームに向けられていましたが、これは可変配列であり、テーブルに未定義の行があります。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";


GuiCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


if (cell == nil) {
    NSArray* views = [[NSBundle mainBundle] loadNibNamed:@"GuiCustomCell" owner:nil options:nil];

    for (UIView *view in views) {
        if([view isKindOfClass:[UITableViewCell class]])
        {
            cell = (GuiCustomCell*)view;
        }
    }   
}

if (timesArray || [timesArray count]) {        

    TakenTime *time = [[TakenTime alloc] init];
    time = [timesArray objectAtIndex:indexPath.row];

    if ([timeFormat isEqualToString:@"seconds"]) {
        cell.detailTextLabel.text = [time stringTimeFormat2];
    } else {
        cell.detailTextLabel.text = [time stringTimeFormat1];
    }

    cell.detailTextLabel.font = [UIFont systemFontOfSize:25];
    cell.textLabel.font = [UIFont systemFontOfSize:18];
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
    cell.textLabel.text = [NSString stringWithFormat:@"%i.",indexPath.row+1];        

}else{

    cell.textLabel.text = @" ";

}

[cell setDefaultText:TRUE];

return cell;

}

4

2 に答える 2

2

(your )に設定cell.textLabel.delegateすると、メソッドで、が次のように配置されているセルに対応するindexPathを取得できます。selfUIViewControllertextFieldDidEndEditing:UITextField

-(void)textFieldDidEndEditing:(UITextField*)aTextField
{
    UITableViewCell* containerCell = (UITableViewCell*)aTextField.superview;
    NSIndexPath* indexPath = [self.tableView indexPathForCell:containerCell];
    // Store the text, for example in an NSMutableDictionary using the indexPath as a key
    [self.mutableDictionaryOfTextValues setValue:aTextField.text forKey:indexPath];
}

もちろん、このtableView:cellForRowAtIndexPath:メソッドでは、に格納されているテキスト値を取得し、[self.mutableDictionaryOfTextValues objectAtIndex:indexPath]それに影響を与えてcell.textLabel.text、以前に入力したテキストを復元します。

あなたのまたは同様の場所のあなたの方法であなたのNSMutableDictionaryself.mutableDictionaryOfTextValues = [NSMutableDictionary dictionary];)をインスタンス化することを忘れないでください。initUIViewController


これは、スクロール前のエディションを終了した場合UITextField、つまりUITextFieldが受信したresignFirstResponder場合(たとえば、キーボードを閉じるか、ユーザーUITextFieldが新しいfirstResponderになる別のキーボードをクリックした場合)にのみ機能することに注意してください。これは、textFieldDidEndEditing:デリゲートメソッドの場合です。と呼ばれます。ユーザーが別のテキストフィールドに移動して起動されるのを待たずに、ユーザーが文字を入力したりテキストを変更したりするたびにテキストを保存する場合は、代わりにデリゲートメソッドtextFieldDidEndEditing:を使用できます。textField:shouldChangeCharactersInRange:replacementString:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // Compute the new text that we will obtain once the new character has been validated
    NSString* newText = [textField.text replaceCharactersInRange:range withString:string];
    // Store it in our dictionary. Same as before, but use newText instead of textField.text
    UITableViewCell* containerCell = (UITableViewCell*)aTextField.superview;
    NSIndexPath* indexPath = [self.tableView indexPathForCell:containerCell];
    // Store the text, for example in an NSMutableDictionary using the indexPath as a key
    [self.mutableDictionaryOfTextValues setValue:newText forKey:indexPath];
    // Accept the new character
    return YES;
}
于 2012-09-01T18:55:39.260 に答える
0

このソリューションは、テキストフィールドにタグを割り当てたことを前提としています。cellForRowAtIndexPathメソッドでタグを宣言できます。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:         (NSIndexPath *)indexPath {
//Code
// Configure the cell...
cell.myTextbox.tag = indexPath.row;
 }

textFieldDidEndEditingメソッドで値を取得します。

-(void)textFieldDidEndEditing:(UITextField *)textField{

if ([textField.text isEqualToString:@""]) {
    textFieldContent[textField.tag] =@"";

}
textFieldContent[textField.tag] = textField.text;
}

次に、cellForRowAtIndexPathメソッドにテキストを設定します。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:      (NSIndexPath *)indexPath {
 MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Mycell" forIndexPath:indexPath];


// Configure the cell...
cell.myLabel.text = [nombres objectAtIndex:indexPath.row];
cell.myTextField.tag = indexPath.row;
if([textFieldContent count] < indexPath.row){
    cell.myTextField.text = @"";

}
else{
if ([textFieldContent count] == 0) {
    cell.myTextbox.text = @"";

}
else{
    cell.myTextbox.text = [textFieldContent objectAtIndex:indexPath.row];
}

}

return cell;
}
于 2014-11-28T22:01:55.923 に答える