cell的滑动删除功能

#define xhwScreenW [UIScreen mainScreen].bounds.size.width

#define xhwScreenH [UIScreen mainScreen].bounds.size.height

#import "CarNumberTableViewController.h"

@interface CarNumberTableViewController ()

@property (nonatomic,strong) NSMutableArray *mArray;

@end

@implementation CarNumberTableViewController

- (void)viewDidLoad {

[super viewDidLoad];

_mArray = [NSMutableArray arrayWithArray:@[@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥",@"钢筋水泥"]];

UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, xhwScreenW, 60)];

searchBar.backgroundColor = [UIColor grayColor];

searchBar.placeholder = @"这里搜索";

[self.view addSubview:searchBar];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return _mArray.count;

}

#pragma mark - 行高

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

return 46;

}

#pragma mark - cell内容

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *indefier = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indefier];

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indefier];

}

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.text = _mArray[indexPath.row];

return cell;

}

//最后,实现UITableView的一些代理方法

//先要设Cell可编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return YES;

}

//定义编辑样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

return UITableViewCellEditingStyleDelete;

}

//进入编辑模式,按下出现的编辑按钮后,进行删除操作

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

if (editingStyle == UITableViewCellEditingStyleDelete) {

[_mArray removeObjectAtIndex:indexPath.row];

// Delete the row from the data source.

[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];

}

}

//修改编辑按钮文字

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath

{

return @"删除";

}

/*

// Override to support rearranging the table view.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

}

*/

/*

// Override to support conditional rearranging of the table view.

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {

// Return NO if you do not want the item to be re-orderable.

return YES;

}

*/

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

你可能感兴趣的:(cell的滑动删除功能)