我在tableview中显示一个项目列表。我需要一次从表中选择和删除多行,有关如何执行此操作的任何资源
答案 0 :(得分:3)
我假设你的桌子只有一个部分。您可以非常轻松地将此解决方案扩展到多个部分。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
中切换indexPath.row在“selectedRows”中的成员身份,如下所示:
NSNumber *rowNsNum = [NSNumber numberWithUnsignedInt:indexPath.row];
if ( [self.selectedRows containsObject:rowNsNum] )
[self.selectedRows removeObject:rowNsNum];
else
[self.selectedRows addObject:rowNsNum];
[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];
}
答案 1 :(得分: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