UITableView左右滑动删除常见问题

对于app应用来讲,使用列表的形式展示数据非UITableView莫属.在熟练掌握了用UITableView展现数据之后,是否是也遇到了须要删除数据的需求?是否是以为在一行数据上划动一下,而后出现一个删除按钮很酷?废话少说,直奔正题,就由笔者来向您展现一下这个功能的实现是多么容易.

先前的准备工做:
第一步,准备好数据源.

#import <UIKit/UIKit.h>
  
@interface UITableCellSwapDeleteViewController : UIViewController <UITableViewDelegate>{
    IBOutlet UITableView *testTableView;
    NSMutableArray *dataArray;
}
@property (nonatomic, retain) UITableView *testTableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@end
  
- (void)viewDidLoad {
    [super viewDidLoad];
    dataArray = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
    
}
这里笔者定义了并实现了一个一维的可变数组.为何要用可变数组呢?由于咱们要删除里面的数据呀.

第二步,展现数据.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}
  
  
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [dataArray count];
}
  
  
// 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] autorelease];
    }
    
    // Configure the cell...
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];
    return cell;
}
经过实现上面三个代理方法向UITableView中添加了数据.


经过上面两步就实现了数据展现工做,接下就实现关键的数据删除了.


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
  
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         [dataArray removeObjectAtIndex:indexPath.row];
         // Delete the row from the data source.
         [testTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
          
     }    
     else if (editingStyle == UITableViewCellEditingStyleInsert) {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
     }    
}
启用上面两个代理,并增长数据删除操做:
[dataArray removeObjectAtIndex:indexPath.row];
在一条数据上向右划动一下.

点Delete.

是否是就成功删除了一条数据呢?
按理说故事讲到这里也就讲完了.可是笔者想延伸一下.注意看图二划动之后的"Delete",你有没有想把这个东东改掉的冲动呢?好比改为:下载?其实很简单,其实下面这个代理方法:

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
        return @"下载";
}





iOS中的UITableView,在上面滑动时,能够出现一个“删除”按钮,来进行删除操做。
可是,有些时候,因为实现方法不对,滑动时,这个按钮却显示不出来。分析了一下缘由,大概以有下几个要素: 数组

1.UITableViewDelegate中的下面一个方法:
  1. // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES.
  2. - (UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath;

这个方法能够不实现,默认会是响应滑动手势,显示“删除”按钮的效果。若是实现的话,必定要返回类型:UITableViewCellEditingStyleDelete。不然在cell上左右滑动时,不会出现“删除”按钮。

2.UITableViewDataSource中的下面两个方法:
  1. // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath:
  2. - (BOOL)tableViewUITableView *)tableView canMoveRowAtIndexPathNSIndexPath *)indexPath;

这个方法表示判断对应的indexPath的cell是否能够编辑。能够不实现,默认是能够编辑的,若是实现的话,必定要返回YES。不然出不来“删除”按钮。

  1. - (void)tableViewUITableView *)tableView commitEditingStyleUITableViewCellEditingStyle)editingStyle forRowAtIndexPathNSIndexPath *)indexPath;
返回你想显示的文本就能够了。

这个方法必定要实现,由于系统会判断你是否实现了这个方法,只有实现了这个方法,在cell上左右滑动时才会出现“删除”按钮。

总之,正确实现上面三个方法,“删除”按钮应该会显示出来。
还要别忘了指定tableView的dataSource和delegate啊,否则一切都是白费的。

小技巧:若是感受“删除”两个字太呆板,能够自定义这个按钮的显示文字的,须要实现UITableViewDelegate的以下方法:
  1. - (NSString *)tableViewUITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPathNSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0);
相关文章
相关标签/搜索