ios - 滑动以删除 TableView 行

标签 ios objective-c uitableview cocoa-touch

我有我的数组:

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

我已经尝试了我能找到的一切,但总是有错误。我希望能够滑动以便出现删除按钮,然后按下删除按钮并删除该行。

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

// 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;

 }

最佳答案

您必须实现必要的 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
    }
}

关于ios - 滑动以删除 TableView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9471642/

相关文章:

objective-c - Xcode 不会创建 iOS .app 发布文件(产品)。如何解决这个问题?

ios - 为什么在使用 prepareToRecord 时出现 AudioConverterNew 错误?

ios - UITableView 没有更新

ios - 如何根据选定的单元格将 segue 推送到特定的 View Controller ?

ios - 如何将 SQLite 文件从 Documents 目录保存回 Objective-C 中的主包?

objective-c - 在 Objective-C 程序中首先调用哪个类?

ios - Objective-C 如何根据字符串比较结果定义宏

uitableview - iOS 11/Xcode 9 : UITableViewCell white background flickers on delete

ios - swift - UITableViewCells 和 SDWebImage 的问题

iphone - 等同于 iphone/iOS 的 [[NSWorkspace sharedWorkspace] runningApplications]?