博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS UITableView删除功能
阅读量:7153 次
发布时间:2019-06-29

本文共 3051 字,大约阅读时间需要 10 分钟。

  UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等。删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell。

 

  使用系统自带删除功能的步骤:

1、让tableView进入编辑状态,也就是设置它的editing为YES

2、返回编辑模式,也就是实现UITableViewDelegate中的tableview:editingStyleForRowAtIndexPath:方法,在里面返回删除模式。如果不实现,默认返回的就是删除模式

3、提交删除操作,也就是实现tableview:commitEditingStyle:editing StyleForRowAtIndexPath:方法。只要实现此方法,就默认实现了系统横扫出现删除按钮的删除方法

4、如果想把删除按钮改为中文,可以实现tableView:titleForDeleteConfirmationButtonForRowAtIndexPath方法

代码:

//  ViewController.m//  JRTableView删除////  Created by jerehedu on 15/6/11.//  Copyright (c) 2015年 jerehedu. All rights reserved.//#import "ViewController.h"#import "Goods.h"@interface ViewController ()
{ UITableView *_tableView; //列表 NSMutableArray *_goodsAry; //商品数组 UIButton *_editBtn; //编辑按钮}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; //添加标题 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)]; titleLabel.text = @"购物车"; titleLabel.textAlignment = NSTextAlignmentCenter; titleLabel.backgroundColor = [UIColor redColor]; titleLabel.textColor = [UIColor whiteColor]; [self.view addSubview:titleLabel]; //添加编辑按钮 _editBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _editBtn.frame = CGRectMake(self.view.frame.size.width-60, 25, 50, 34); [_editBtn setTitle:@"编辑" forState:UIControlStateNormal]; [_editBtn setTitle:@"完成" forState:UIControlStateSelected]; _editBtn.titleLabel.font = [UIFont systemFontOfSize:15]; _editBtn.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5]; [self.view addSubview:_editBtn]; [_editBtn addTarget:self action:@selector(clickEditBtn:) forControlEvents:UIControlEventTouchUpInside]; //添加tableview _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-64)]; _tableView.dataSource = self; _tableView.delegate = self; [self.view addSubview:_tableView]; //取数据 NSArray *ary = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ShoppingGoodsList" ofType:@"plist"]]; //把数据存到模型对象中,然后把对象存到数组中 _goodsAry = [NSMutableArray array]; for (int i=0; i
@interface Goods : NSObject@property (nonatomic, copy) NSString *icon;@property (nonatomic, copy) NSString *name;@property (nonatomic, copy) NSString *details;-(id)initWithDic:(NSDictionary*)dic;+(id)goodsWithDic:(NSDictionary*)dic;@end#import "Goods.h"@implementation Goods-(id)initWithDic:(NSDictionary *)dic{ if (self = [super init]) { self.icon = [dic objectForKey:@"icon"]; self.name = [dic objectForKey:@"name"]; self.details = [dic objectForKey:@"details"]; } return self;}+(id)goodsWithDic:(NSDictionary *)dic{ Goods *good = [[Goods alloc] initWithDic:dic]; return good;}@end

 

  疑问咨询或技术交流,请加入官方QQ群: (452379712)

 

作者:
出处:
 
本文版权归和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
 

转载于:https://www.cnblogs.com/jerehedu/p/4703968.html

你可能感兴趣的文章
[LeetCode] Find Right Interval 找右区间
查看>>
android View 绘制完成监听
查看>>
igbinary vs serialize vs json_encode
查看>>
Snipaste强大离线/在线截屏软件的下载、安装和使用
查看>>
禅与摩托车维修的艺术
查看>>
elasticsearch term match multi_match区别
查看>>
前端学PHP之PDO预处理语句
查看>>
webservice理解
查看>>
TestNG的简单使用
查看>>
数组可以容纳多少水----------给你出道题
查看>>
Linux之sar命令介绍
查看>>
飘逸的python - 增强的格式化字符串format函数
查看>>
纯html页面之间传参
查看>>
linux 如何查找io的进程
查看>>
AE控制图层中要素可见状态的几种方法
查看>>
Nginx之http_image_filter_module模块使用
查看>>
重温设计模式
查看>>
js dorado
查看>>
C#枚举
查看>>
ORACLE11g中毒恢复
查看>>