滑动以删除TableView行

我有我的arrays:

self.colorNames = [[NSArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil]; 

我已经尝试了所有可以找到的东西,但总会出现错误。 我希望能够滑动以便显示删除按钮,然后按删除按钮并删除该行。

完整代码(与表有关的所有内容):

 // HEADER FILE @interface ViewController : UIViewController  { IBOutlet UITableView *tableView; NSMutableArray *colorNames; } @property (strong, nonatomic) NSArray *colorNames; @end // IMPLEMENTATION #import "ViewController.h" @implementation ViewController @synthesize colorNames; - (void)viewDidLoad { [super viewDidLoad]; self.colorNames = [[NSMutableArray alloc] initWithObjects:@"Red", @"Green", @"Blue", @"Indigo", @"Violet", nil]; [super viewDidLoad]; //[tableView setEditing:YES animated:NO]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } // Customize the number of rows in the table view. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { int count = [colorNames count]; return count; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } // Configure the cell. cell.textLabel.text = [self.colorNames objectAtIndex: [indexPath row]]; return cell; } 

您必须实现必要的UITableViewDelegateUITableViewDataSource方法。

首先,添加:

 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } 

然后:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { //remove the deleted object from your data source. //If your data source is an NSMutableArray, do this [self.dataArray removeObjectAtIndex:indexPath.row]; [tableView reloadData]; // tell table to refresh now } } 

首先,您的colorNames应该是NSMutableArray而不是NSArray。 您无法在常规(不可变)数组中添加或删除对象; 每次进行更改时都必须重新创建它。 切换将使这更容易。 对于-tableView:commitEditingStyle:forRowAtIndexPath: ,您将能够执行以下操作:

 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if(editingStyle == UITableViewCellEditingStyleDelete) { [colorNames removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; } } 

实现以下方法:

 - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } 

Swift 3和Swift 4在不使用reloadData的情况下回答

我更喜欢使用deleteRows而不是使用reloadData。

 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { // Enables editing only for the selected table view, if you have multiple table views return tableView == yourTableView } func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { if editingStyle == .delete { // Deleting new item in the table yourTableView.beginUpdates() yourDataArray.remove(at: indexPath.row) yourTableView.deleteRows(at: [indexPath], with: .automatic) yourTableView.endUpdates() } } 

这对我有用

 -(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{ if (editingStyle == UITableViewCellEditingStyleDelete){ Comment *comment = [self.commentArray objectAtIndex:indexPath.row]; dispatch_async(kBgQueue, ^{ if([self.thePost deleteCommentForCommentId:comment.commentId]){ if (indexPath.row < self.commentArray.count) { [self.commentArray removeObjectAtIndex:indexPath.row]; } dispatch_async(dispatch_get_main_queue(), ^{ [self.tvComments reloadData]; }); } }); } [self.tvComments reloadData]; } 

Swift版本:

 func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == UITableViewCellEditingStyle.Delete { dataArray?.removeAtIndex(indexPath.row) tableview.reloadData() } } 

Swift 4版本:

 func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { let delete = UITableViewRowAction(style: .destructive, title: "delete") { (action, indexPath) in // delete item at indexPath } return [delete] }