1

更新:残りのセルを順番に再タグ付けすることで、この問題を解決しました。 for ループでそれらを順番に再タグ付けし、残りのデータ量まで毎回インデックスパスの行をインクリメントします。

2 つのセクションを持つ UITableView があります。1 つのセクションが固定されます。もう一方のセクションは動的に変化し、ユーザーがボタンをクリックすると、新しい行が追加されます。セルには、ユーザーが編集できる 3 つのテキスト ボックスが含まれています (タグ 1、2、および 3)。このセクションの行は編集可能で、ユーザーはスワイプして削除できます。現在、この機能はすべて正常に動作しています。UI は次のとおりです。

ここに画像の説明を入力

うまく機能しないのは、データ ソースを更新しようとしているときです。「ReceiptItem」というカスタム クラスのオブジェクトを含む配列を使用します。ユーザーがセル内の UITextFields の 1 つを編集すると、"ReceiptItem" 内の対応するプロパティが更新されます。現在、タグを使用して、編集中のセルとテキスト フィールドを追跡しています。

編集中のセルとテキストフィールドを特定するコードは次のとおりです。

- (void)textFieldDidEndEditing:(UITextField *)textField{
    //find the cell and textfield that just ended editing
    int row = textField.superview.tag;
    int column = textField.tag;

    ReceiptItem *itemToBeUpdated = [[ReceiptItem alloc]init];
    itemToBeUpdated = [receiptItemsArray objectAtIndex:row];

    //update the receiptItem
    switch (column) {
        case 1:
            //quantity text field
            itemToBeUpdated.quantityValue = [textField.text doubleValue];
            break;
        case 2:
            //item text field
            if ([textField text].length == 0) {
                itemToBeUpdated.itemName = @"Blank";
            }
            else{
                itemToBeUpdated.itemName = [textField text];
            }
            break;
        case 3:
            //price text field
            itemToBeUpdated.priceValue = [textField.text doubleValue];
            break;
    } 
    [receiptItemsArray replaceObjectAtIndex:row withObject:itemToBeUpdated];
}

問題が発生するのは、行が削除されたときです。対応するオブジェクトをデータソースから削除すると、配列のカウントが 1 つ減ります。次に、削除されたセルからさらに下のセルのテキストフィールドを編集しようとすると、セルのタグが配列の数よりも大きくなるため、「範囲外」エラーが発生します。それはこの行で起こっています(上から):

itemToBeUpdated = [receiptItemsArray objectAtIndex:row];

a)セルをより適切に追跡する方法、b)セルが削除されたときにセルにタグを付け直す方法、またはc)何か他の方法を見つけようとしています。答えはありますか?必要に応じて、セルを削除するコードを投稿できます。

4

1 に答える 1

1

if (cell == nil) のように、 cellForRowAtIndexPath のメソッドで textField にタグを付けていると思います。

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

            if (cell == nil) {
                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]
yourTextField.tag = indexPath.row;            
            }
}

行を削除してデータをリロードするときに textField が次のような新しいタグを取得するように、if ブロックの外側でタグ付けを行います。

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

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

                }
yourTextField.tag = indexPath.row;
return cell;
    }
于 2012-12-31T08:07:32.600 に答える