1

テーブルビューにアイテムのリストを表示しています。一度にテーブルから複数の行を選択して削除する必要があります。これを行う方法に関するリソース

4

2 に答える 2

3

あなたのテーブルにはセクションが1つしかないと仮定しています。このソリューションを複数のセクションに簡単に拡張できます。

  • NSMutableSet メンバー「selectedRows」を、TableView を管理する UIViewController サブクラスに追加します。
  • 次のように、「selectedRows」で- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPathindexPath.row のメンバーシップを切り替えます。

NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowNsNum] )
    [self.selectedRows removeObject:rowNsNum];
else 
    [self.selectedRows addObject:rowNsNum];
  • 行が選択されていることを視覚的に示す (たとえば、セルの accessoriesType プロパティを UITableViewCellAccessoryCheckmark に設定する)、または他の方法でセルを視覚的に変更して、選択された行であることを示す
  • 「deleteRows」というセレクターに接続された、テーブル セクションのヘッダー/フッター、タイトル バーなどの任意の場所に「削除」ボタンを UI に追加します。
  • deleteRows メソッドで、selectedRows セットを反復処理して indexPath の配列を作成し、データ モデルからこれらの行を削除してから、(好みのアニメーション タイプで) を呼び出します。

[self.myTableView deleteRowsAtIndexPaths:arrayOfIndexPathsToDelete withRowAnimation:UITableViewRowAnimationTop];    

編集:これが私の完全な didSelectRowAtIndexPath メソッドです。正しい操作のために deselectRowAtIndexPath が必要になる場合があります。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
    if ( self.editing )
        return;

    [self.myTableView deselectRowAtIndexPath:indexPath animated:YES];

    NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
    if ( [self.selectedRows containsObject:rowNsNum] )
        [self.selectedRows removeObject:rowNsNum];
    else 
        [self.selectedRows addObject:rowNsNum];

    [self.myTableView performSelector:@selector(reloadData) withObject:nil afterDelay:0.2];
}
于 2011-02-10T07:39:33.470 に答える
0
#import "ViewController.h"
#import "TableViewCell.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tblView;
@property (strong,nonatomic)NSMutableArray *arryData1,*arryData2;
@property (strong,nonatomic)UIBarButtonItem *edit,*delete;
@end

@implementation ViewController
{
    BOOL Selected;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arryData1 = [[NSMutableArray alloc] initWithObjects:@"MCA",@"MBA",@"BTech",@"MTech",nil];
        self.arryData2 = [[NSMutableArray alloc] initWithObjects:@"Objective C",@"C++",@"C#",@".net",nil];

    self.edit=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(edit:)];
    self.navigationItem.leftBarButtonItem=self.edit;
    self.delete=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(delete:)];
    self.navigationItem.rightBarButtonItem=self.delete;
    self.tblView.allowsMultipleSelection=YES;

    }

-(void)edit:(id)sender
{
    Selected=YES;
    [self.tblView reloadData];
}
-(void)delete:(id)sender
{
    NSArray *selectedCells = [self.tblView indexPathsForSelectedRows];
    NSMutableIndexSet *indicesToDelete = [[NSMutableIndexSet alloc] init];
    for (NSIndexPath *indexPath in selectedCells) {
        [indicesToDelete addIndex:indexPath.row];
    }
    //arrayFromPlist is NSMutableArray
    [self.arryData1 removeObjectsAtIndexes:indicesToDelete];
    [self.tblView beginUpdates];
    [self.tblView deleteRowsAtIndexPaths:selectedCells withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tblView endUpdates];

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove the row from data model
    [self.arryData1 removeObjectAtIndex:indexPath.row];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [self.arryData1 count];
    }



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

        TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil)
        {
            cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
        }
        if (Selected==YES) {
            cell.imageView.image=[UIImage imageNamed:@"trash.png"];
        }
        else
        {
            cell.imageView.image=nil;
        }
        cell.textLabel.text = [self.arryData1 objectAtIndex:indexPath.row];
        cell.detailTextLabel.text = [self.arryData2 objectAtIndex:indexPath.row];

        return cell;
    }

@end
于 2016-02-02T07:47:40.303 に答える