2011-06-24 24 views
2

私はiPadのアプリケーションを開発しています.1つの画面に複数のセクションを持つ埋め込みテーブルビューがあります。各セクションには、独自の配列(array1およびarray2)が設定されます。複数のセクションを持つUITableViewから行を削除する

この表を編集モードにするボタンを作成しました。しかし、私は、選択された行はである何セクション決定し、同様に関連した配列からエントリを削除するには、私の

何とか

$tableView:commitEditingStyle:forRowAtIndexPath

を変更する必要があります。誰にもアイデアはありますか? ...働くかもしれないセクションにswitchステートメントを使用して、正しい配列から行を削除する必要があるよう

NSInteger section = [indexPath section]; 
if (section == 0) 
    { 
     // write your code for deleting rows in your 1st section 
     } 

if (section == 1) 
    { 
     // write your code for deleting rows in your 2nd section 
     } 

    // This is the idea... 

答えて

3

が見える使用して必要な部分...を取得します(このよう

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ 
    if(editingStyle == UITableViewCellEditingStyleDelete){  
//This is the line i need to change... 
     [array1 removeObjectAtIndex:indexPath.row]; 
     [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 
+0

感謝を。すべてのソリューションが機能しましたが、SwitchMethodがこの問題を解決する最良の方法です。 – ephilip

2

います設定された数のセクションがある場合にのみ表示されます)

オブジェクトをアレイから削除する場合は、そのセクションに基づいて正しいアレイからオブジェクトを削除してください。

switch (indexPath.section) { 
    case 0 
     [array0 removeObjectAtIndex:indexPath.row]; 
     break; 
    case 1 
     [array1 removeObjectAtIndex:indexPath.row]; 
     break; 
    } 

など

1
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

あなたが選択されているセルを決定する必要がある方法です。

あなたはちょうどあなたがindexPath.section

NSInteger section = [indexPath section]; 
2

でindexPath変数は、プロパティの行やセクションを持っていることを行うことができます示されている実際のセクションを決定します。だから、あなたが行うことができます:

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

    if (indexPath.section == 1) { 
     // do something 
    } 
} 
0

commitEditingStyleのindexPathパラメータ:...メソッドセルのhas the section and row。おそらく私は疑問を誤解しています。関連する配列から削除することによって何を意味するのかを明確にすることができますか?テーブルの各セクションに1つの配列がありますか?

1

これを試してみてください:

[[self.mainArrayList objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row]; 
関連する問題