2012-02-14 8 views
0

私にはtableviewControllerがあります。 UINavigationBarには、「編集」というボタンが追加されます。ユーザーがそれをクリックすると、すべてのセルを編集モードにする必要があります。そこではレコードを削除できます。テーブルからセルを削除する - コードが提供されました

次のような方法があります。私は正しいですか?編集ボタンをクリックすると、赤い円とセルの削除ボタンが表示されますか?

2.)編集ボタン(UIBarbuttonitem)のコードをユーザーがクリックすると、次のメソッドを呼び出す方法を教えてください。

3.)セルを削除すると、テーブルのデータをリロードする必要があります。私はこれのためのコードを書いた、それは正しいですか? (私は現時点でMacにはない)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

     [self.tableView beginUpdates]; 
     [discardedItems addObject:[self.entries objectAtIndex:indexPath.row]]; 
     [self.itemsMutableArray removeObjectsInArray:discardedItems ]; 
     self.entries = [NSArray arrayWithArray:self.itemsMutableArray]; 
     [self.tableView endUpdates]; 

     [self.tableView reloadData]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
+0

itemsMutableArrayは何のために使用しますか?あなたは変更可能な配列としてエントリを持つことはできませんか? – SEG

+0

'entries'配列の内容をコピーしましたので、簡単に項目を削除してから' entries'配列に再割り当てすることができました。 – shajem

答えて

1

まず、行を編集できるかどうかを指定する必要があります。これは、次のメソッドによって行われます。

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 

すべての行を編集可能にする場合は、yesを返します。赤い円はこれを使用して取得するには

、編集ボタンから呼び出されるメソッドで、おそらく

...(のviewDidLoadで)

UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit ", @"") style:UIBarButtonItemStyleBordered target:self action:@selector(pressedEdit:)]; 
self.navigationItem.leftBarButtonItem = editButton; 

とpressedEditで:

- (void) pressedEdit :(id)sender { 
    UIBarButtonItem *editButton = (UIBarButtonItem*)self.navigationItem.leftBarButtonItem; 

    if (!self.tabeView.editing) { 
     [self.tableView setEditing:YES animated:YES]; 
     editButton.title = NSLocalizedString(@"Done", @""); 
    } 
    else { 
     [self.tableView setEditing:NO animated:YES]; 
     editButton.title = NSLocalizedString(@"Edit", @""); 
    } 
} 

あなたが書いたコードによると、私はあなたが最初にdatasourを更新すべきだと思いますセルを取り除いてください。

+0

どこで '[self.tableView setEditing:YES animated:YES];'を書くべきですか? 'viewdidLoad'メソッドの中にありますか?私は '[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];を含めるべきですか? '内部' [self.tableView beginUpdates]; '?? – shajem

+0

deleteRowsAtIndexPathsは、データ配列から削除した後にする必要があります。 setEditing:赤い円を表示するときに呼び出す必要があります。 beginUpdatesとendUpdatesも必要ではないと思います – SEG

+0

UIBarbuttonItemを作成するときに、 'Action'属性に何を追加する必要がありますか? (どのメソッドを実行すべきですか)? – shajem

1

あなたのコードは間違っていないようです。しかし、ほとんど仕事がないので、非常に多くのライン。あなたもreloadDataでアニメーションを終了させます。また、beginUpdates/endUpdatesのtableViewに触れない呼び出しをラップします。

最初にNSMutableArrayをデータソースとして使用してみませんか?あなたは2つになってコードの7行を減らすことができます:

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // delete the item from the datasource 
     [self.entries removeObjectAtIndex:indexPath.row]; 
     // Delete the row from the table view 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
+0

おそらく、彼はdiscardedItems配列の削除された項目を追跡したいと思っています。しかし、私はあなたに完全に同意します... – SEG

+0

はい、私は削除されたレコードを追跡する必要があります。しかし、私はまだ私が上記のメソッドを追加する 'edit'uibarbuttonをクリックした場合、コードがどのように知っているか知りたいですか? – shajem

関連する問題