2012-03-01 5 views
0

が、私はそれが完璧に動作しますのUITableView編集が

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

    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
     [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
    } 
} 

データベースからセルの内容を削除するため、このコードを持っていますが、私は私のボタンのクリックで上記のコードを配置する必要があり疑問、私はクリックボタンでこれを入れ

-(IBAction)_clickbtndeltNoteall:(id)sender 
{ 
      [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
      [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
      [tableViewObj reloadData]; 
      NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
      [appDelegate.data writeToFile:DataPath atomically:YES]; 
      [tableViewObj reloadData]; 
} 

しかし、私はこのエラーを受け取りました: "宣言されていない識別子のインデックスパス"。これを解決するには?

+0

挑戦的にあなたは1つのNSIndexPathオブジェクトを.hファイルに入れ、commitEditingStyleメソッドで値を割り当て、ボタンアクションメソッドでそのオブジェクトを使用します。 – Narayana

答えて

1

押された行のindexPathをシングルトンに保存することをお勧めします。 singleton classを使用してオブジェクトを格納し、クラス間でオブジェクトを使用する方法についてのガイドです。

この例ではあなたはまた、単にインスタンス変数を宣言し、行があなたののUITableViewのための適切なデリゲートメソッドに押されたときにそれを設定することができます。これには、同様に行うことができる:

@interface 

NSIndexPath *tableViewPath; 

@implementation 

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

tableViewPath = indexPath; 

} 

あなたのボタンのあなたのアクションは次のようになります。

ボタンのコード行が選択されていることを前提とし

- (IBAction)_clickbtndeltNoteall:(id)sender 
{ 
     [appDelegate.indexArray removeObjectAtIndex:indexPath.section]; 
     [appDelegate.notesArray removeObjectAtIndex:indexPath.section]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
} 
押される前にまた、そのよう のviewDidLoad であなたの indexPathを初期化できます。

@implementation 

- (void)viewDidLoad { 

tableViewPath = [NSIndexPath indexPathForRow:0 inSection:0]; 

} 

あなたが特定のプログラムのために適切であろうものに、あなたの初期設定を調整する必要があります!

1

ボタンをクリックしたときに、どのセルを削除しますか?

追加:あなたの方法の上に

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowToDelete inSection:sectionToDelete]; 

+0

それは明らかに良い解決策ではありません。削除する必要がありますか?また、NSIndexPathのローカル宣言は悪い考えです。このためにインスタンス変数を使用する必要があります。 – wizH

+0

あなたは自分のコントローラにNSIndexPathインスタンスを保持して、削除するインデックスを知るだけでいいのですか?私は彼がNSIndexPathを必要としないと同意しますが、インスタンス変数を使用しないでください。 – fbernardo

+0

彼はクラス全体の行のインデックスを保持したい場合、それは理想的だろうと思いますか? – wizH

1

indexPath_clickbtndeltNoteallに定義されていないため、そのエラーが発生します。

あなたはindexがどこか別の場所に定義されて

-(IBAction)_clickbtndeltNoteall:(id)sender 
{ 
     [appDelegate.indexArray removeObjectAtIndex:index]; 
     [appDelegate.notesArray removeObjectAtIndex:index]; 
     [tableViewObj reloadData]; 
     NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
     [appDelegate.data writeToFile:DataPath atomically:YES]; 
     [tableViewObj reloadData]; 
} 

(例えばクラスフィールド)のような何かを行うことができます。ので、(ID)、送信者、それは不可能です:(IBAction)_clickbtndeltNoteall - あなたは、あなたの内側にindexPath変数にアクセスしている_clickbtndeltNoteall

0

を呼び出し、その後、削除したい部分を指定しindexを設定するために補助メソッドを使用することができます変数はそのスコープに存在しません。

最後のindexPathをグローバル変数に格納してアクセスしないのはなぜですか?このような何か:

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

     if (editingStyle == UITableViewCellEditingStyleDelete) {  
     lastIndexPath = indexPath; 
     //Your code 
     } 
} 

そして、あなたのinterfaz宣言で、あなたが入れ:ボタンは、ボタンが上にないitself..Ifセル上にある場合

NSIndexPath *lastIndexPath; 
0
-(IBAction)_clickbtndeltNoteall:(id)sender 
{  
UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
    NSIndexPath *clickedButtonPath = [tableViewObj indexPathForCell:clickedCell]; 
    int section1=clickedButtonPath.section; 
[appDelegate.indexArray removeObjectAtIndex:section1]; 
      [appDelegate.notesArray removeObjectAtIndex:section1]; 
      [tableViewObj reloadData]; 
      NSString *DataPath = [MyBibleAppAppDelegate getPath]; 
      [appDelegate.data writeToFile:DataPath atomically:YES]; 
      [tableViewObj reloadData]; 
} 

をそれは確かに動作しますセルを指定すると、直接uitableviewcellを指定せずにindexpathを渡すことができます。試してみてください。

関連する問題