ios 滑动以删除 TableView 行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9471642/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swipe To Delete TableView Row
提问by JTApps
I have my array:
我有我的阵列:
self.colorNames = [[NSArray alloc]
initWithObjects:@"Red", @"Green",
@"Blue", @"Indigo", @"Violet", nil];
I've tried everything I can find, but there's always errors. I want to be able to swipe so that the delete button appears, and then press the delete button and have that row removed.
我已经尝试了我能找到的一切,但总是有错误。我希望能够滑动以便出现删除按钮,然后按下删除按钮并删除该行。
Full code (everything relating to the table):
完整代码(与表格相关的所有内容):
// HEADER FILE
@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
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;
}
回答by edc1591
You have to implement the necessary UITableViewDelegateand UITableViewDataSourcemethods.
你必须执行必要UITableViewDelegate和UITableViewDataSource方法。
First, add this:
首先,添加以下内容:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
Then:
然后:
- (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
}
}
回答by Noah Witherspoon
For a start, your colorNamesshould be an NSMutableArray rather than an NSArray. You can't add or remove objects from a regular (non-mutable) array; you'd have to recreate it each time you made a change. Switching that will make this easier. For your implementation of -tableView:commitEditingStyle:forRowAtIndexPath:, you'll then be able to do something like this:
首先,你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];
}
}
回答by Ash Furrow
Implement the following method:
实现以下方法:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
回答by Anand
Swift 3 and Swift 4 answer without using reloadData
Swift 3 和 Swift 4 在不使用 reloadData 的情况下回答
I prefer using deleteRows instead of using 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()
}
}
回答by Pratik Lad
Swift 4 Version :
斯威夫特 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]
}
回答by NSGodMode
This is what worked for me
这对我有用
-(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];
}
回答by Irfan
Swift Version:
迅捷版:
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete {
dataArray?.removeAtIndex(indexPath.row)
tableview.reloadData()
}
}

