IOS--添加底部工具栏和UITableViewCell的滑动删除 推荐
2013-07-15 09:59
246 查看
添加底部工具栏和UITableViewCell的滑动删除一.添加底部工具栏
在工具栏上设置图标时可以使用系统自带图标也可以使用自定义图标,下面分别介绍这两种方法:
1.使用系统自带图标(编辑图标)
UIBarButtonItem *editItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemComposetarget:selfaction:@selector(editEventClick)];2.使用自定义图标(删除图标)
UIBarButtonItem *deleteItem = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"ic_delete.png"] style:UIBarButtonItemStylePlaintarget:selfaction:@selector(deleteEventClick)];工具栏提示:
1.固定空间可以拥有宽度-----在所有UIBarButtonItem’中,只有UIBarButtonSystemItemFixedSpace条目可以被分配一个宽度,因此,创建空间条目,设置其宽度,然后在将其添加到条目列中。UIBarButtonItem *fixItem1 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];fixItem1.width = 125; NSArray *arrayItem = [NSArrayarrayWithObjects:fixItem1, editItem,fixItem2,deleteItem,nil];
[selfsetToolbarItems:arrayItem];
此处编辑图标是自定义的图片,删除图标是系统自带,在截图中可以看出二者的区别运行之后效果图如下:
![](http://blog.51cto.com/attachment/201307/095217487.png)
2.使用一个灵活空间进行左或右对齐-----在条目列表中开始添加一个’ UIBarButtonSystemITemFlexibleSpace会使所有剩余条目右对齐。在末尾添加一个,则左对齐,使用两个UIBarButtonSystemItemFlexibleSpace,一个在开头,一个添加在末尾,会使剩余条目居中对齐。UIBarButtonItem *fixItem1 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil]; NSArray *arrayItem = [NSArrayarrayWithObjects:fixItem1,deleteItem,fixItem2,editItem,fixItem2,nil];
[selfsetToolbarItems:arrayItem];
运行之后效果图如下:
![](http://blog.51cto.com/attachment/201307/095217349.png)
(此处编辑图标和删除图标都是系统自带)
二.滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
returnYES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;
}
//进入编辑模式
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath {//code
}
启用上面两个代理,并增加数据删除操作:运行之后效果如下:
![](http://blog.51cto.com/attachment/201307/095217549.png)
若让其左滑删除,则:(核心代码如下)
//取消TableView的右滑,保留左滑
UISwipeGestureRecognizer *swipeRight =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:nil];
swipeRight.direction =UISwipeGestureRecognizerDirectionRight;
[self. tableView addGestureRecognizer:swipeRight];
在工具栏上设置图标时可以使用系统自带图标也可以使用自定义图标,下面分别介绍这两种方法:
1.使用系统自带图标(编辑图标)
UIBarButtonItem *editItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemComposetarget:selfaction:@selector(editEventClick)];2.使用自定义图标(删除图标)
UIBarButtonItem *deleteItem = [[UIBarButtonItemalloc] initWithImage:[UIImageimageNamed:@"ic_delete.png"] style:UIBarButtonItemStylePlaintarget:selfaction:@selector(deleteEventClick)];工具栏提示:
1.固定空间可以拥有宽度-----在所有UIBarButtonItem’中,只有UIBarButtonSystemItemFixedSpace条目可以被分配一个宽度,因此,创建空间条目,设置其宽度,然后在将其添加到条目列中。UIBarButtonItem *fixItem1 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpacetarget:nilaction:nil];fixItem1.width = 125; NSArray *arrayItem = [NSArrayarrayWithObjects:fixItem1, editItem,fixItem2,deleteItem,nil];
[selfsetToolbarItems:arrayItem];
此处编辑图标是自定义的图片,删除图标是系统自带,在截图中可以看出二者的区别运行之后效果图如下:
![](http://blog.51cto.com/attachment/201307/095217487.png)
2.使用一个灵活空间进行左或右对齐-----在条目列表中开始添加一个’ UIBarButtonSystemITemFlexibleSpace会使所有剩余条目右对齐。在末尾添加一个,则左对齐,使用两个UIBarButtonSystemItemFlexibleSpace,一个在开头,一个添加在末尾,会使剩余条目居中对齐。UIBarButtonItem *fixItem1 = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil]; NSArray *arrayItem = [NSArrayarrayWithObjects:fixItem1,deleteItem,fixItem2,editItem,fixItem2,nil];
[selfsetToolbarItems:arrayItem];
运行之后效果图如下:
![](http://blog.51cto.com/attachment/201307/095217349.png)
(此处编辑图标和删除图标都是系统自带)
二.滑动删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
returnYES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
returnUITableViewCellEditingStyleDelete;
}
//进入编辑模式
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyleforRowAtIndexPath:(NSIndexPath *)indexPath {//code
}
启用上面两个代理,并增加数据删除操作:运行之后效果如下:
![](http://blog.51cto.com/attachment/201307/095217549.png)
若让其左滑删除,则:(核心代码如下)
//取消TableView的右滑,保留左滑
UISwipeGestureRecognizer *swipeRight =[[UISwipeGestureRecognizer alloc]initWithTarget:self action:nil];
swipeRight.direction =UISwipeGestureRecognizerDirectionRight;
[self. tableView addGestureRecognizer:swipeRight];
相关文章推荐
- iOS UITableViewCell滑动删除
- iOS UITableViewCell滑动删除
- 【iOS-Cocos2d游戏开发之七】添加/删除系统组件,并解决View设置透明会影响View中的其他组件的问题! 推荐
- iOS UITableView的细致功能(滑动删除,拖动排序,添加行,搜索过滤)
- iOS中uitableview滑动删除添加按钮
- iOS - UITableViewCell滑动删除
- IOS第13天(3,私人通讯录,登陆状态数据存储,数据缓存, cell的滑动删除,进入编辑模式,单个位置刷新 )
- iOS 之 使用百度地图 (删除地图页面所有自己添加的标注)
- JS实现动态表格的添加,修改,删除功能(推荐)
- js遍历添加栏目类添加css 再点击其它删除css【推荐】
- UITableViewCell滑动删除及移动
- iOS之UITableViewCell左右滑动效果
- Spring3整合Quartz2实现定时任务及动态任务调整(添加删除暂停恢复)--推荐
- IOS中http请求的cookie查看,删除,添加
- iOS--- UITableView 底部添加按钮
- 为项目添加滑动删除效果
- android 下拉刷新上拉加载更多,高仿ios左滑动删除item,解决了众多手势问题
- iOS-判断webView是否滑动到底部
- 【iOS-Cocos2d游戏开发之十一】使用Box2d物理系统以及在cocos2d框架添加Box2d物理系统lib包的方法 推荐
- 仿ios短信列表滑动出现删除按钮