滑动以删除TableView行

JTA*_*pps 28 cocoa-touch objective-c uitableview ios

我有我的阵列:

self.colorNames = [[NSArray alloc] 
initWithObjects:@"Red", @"Green",
@"Blue", @"Indigo", @"Violet", nil];
Run Code Online (Sandbox Code Playgroud)

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

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

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

 }
Run Code Online (Sandbox Code Playgroud)

edc*_*591 128

你必须实现必要的UITableViewDelegateUITableViewDataSource方法.

首先,添加:

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

然后:

- (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
    }
}
Run Code Online (Sandbox Code Playgroud)

  • Aslo添加[tableView reloadData]; 就在方法的最后一个括号之前. (5认同)

Noa*_*oon 21

首先,您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];
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 用于[tableView deleteRowsAtIndexPaths]的+1以进行UI更新以及数据源 (2认同)

Ash*_*row 6

实现以下方法:

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
Run Code Online (Sandbox Code Playgroud)