2011-12-06 7 views
0

私のテーブル(UITableViewUITextField isteadがUILabelのセルを「addSubview」でセルに追加しました。私は自分の細胞を直接編集可能にしたいので、これが必要です。セルスタイルの私はUITableViewCellStyleDefaultを使用します。 - すべてうまく動作します:いつでもセルを追加したり編集したりできます。テーブルのセルを削除する際の問題

ただし、削除すると問題が発生します。セルを「削除」してテーブルをreloadDataにすると、セルには古いコンテンツと新しいコンテンツが表示されます。その下のすべてのセルについても同じです。私は、私のアプリケーションを閉じて、もう一度起動すると、テーブルが正しく表示されます。ここで

私はセル

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
              forRowAtIndexPath:(NSIndexPath *)indexPath { 

NSUInteger row = [indexPath row]; 
NSUInteger count = [datas count]; 

if (row <= count) { 

    NSString* data = [datas objectAtIndex: [indexPath row]]; 
    [self deleteDatas:data]; 
} 

[self.locationTable reloadData]; 

}新しい負荷によって「prooved」としてdeleteDatasに私は、正常に動作するファイルから該当する件のデータを削除

に削除するために使用するコードアプリ。ここで

-(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]; 
    UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; 
    longPressGesture.minimumPressDuration = 2.0; 
    [cell addGestureRecognizer:longPressGesture]; 
} 

// Configure the cell. 

// table cell with uitextfield instead of lable. 
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 185, 30)]; 
textField.enabled = NO; 
[cell.contentView addSubview:textField]; 

NSUInteger count = [datas count]; 
NSUInteger row = [indexPath row]; 

// last cell is empty to edit it 
if (row+1 < count) { 

    textField.text = [datas objectAtIndex:[indexPath row]]; 
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
} 

return cell; 

}

任意のアイデア? - ありがとう

私のセルが2倍のコンテンツを表示する理由は何ですか(元のセルの1回と1回のセルの内容?) - 私は思っています。セルを再ロードすると間違っています。 - テキストフィールドが問題を起こす可能性はありますか? - どうすればわかるの?以上のように方法:

+0

はあなたが呼ばれるあなたの方法で記述したコード投稿することができます - (無効)にtableView:(のUITableView *)のtableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath – Tendulkar

+0

をしても、あなたのIBOutlet接続を確認し – Tendulkar

+1

ここにあなたのcellFroRowAtIndexPathを貼り付けます。 –

答えて

0

...

NSString* data = [datas objectAtIndex: [indexPath row]]; 
[self deleteDatas:data]; 
[datas removeObjetAtIndex: [indexPath row]]; 

そうでない文字列が...

メモリにまだそこにあるとcellForRowに表示されている次回のreloadDataで
+0

は 'deteDatas:data 'ですでに実行されています。そこに私は "datas"新しい "作成" – user1075871

1

あなたはおそらくあなたのcommitEditingStyleを書く必要があります。その後、そうでない場合は、正しいデータを返す:numberOfRowsInSection:チェックする

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
    if (indexPath.row <= [data count]) { 
     // Update the model by deleting the actual data 
     NSString* data = [datas objectAtIndex:indexPath.row]; 
     [self deleteDatas:data]; 
     // Delete the row from the table 
     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationBottom]; 
    } 
    } 
} 

他のものを、のtableViewのためのあなたの方法があることですテーブルが行を削除しようとするとアサーションエラーが発生し、ロジックが足りなくなります。私はあなたにもcommitEditingStyleで、あなたのmutablearrayから要素を削除するべきだと思い

+0

コードはアサーションの失敗につながるので、私の 'numberOfRowsInSection 'は間違っていますか? – user1075871

+0

ええ、おそらく。 deleteDatas:は何を貼り付けることができますか?なぜ単に[データremoveObjectAtIndex:indexPath.row]を実行しないのですか? –

関連する問題