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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,985评论 3 38
  • 1.nav1.navigationBar.barStyle=UIBarStyleBlack; //改变导航栏背景颜...
    SadMine阅读 5,688评论 1 4
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 5,330评论 9 5
  • 哦吼吼,又研究了几天,把FMDB这个封装好的数据库搞定了,写了个简单的例子,基于FMDB的添删改查操作,界面很一般...
    lichengjin阅读 3,677评论 0 0
  • 作者唯一QQ:228544117。。。。。 =========后面的都要新建一个文章 AppDelegate.h ...
    CC_iOS阅读 4,744评论 0 0

友情链接更多精彩内容