#import "ViewController.h"

@interface
ViewController () <UITableViewDataSource,
UITableViewDelegate>

@property (nonatomic,
retain) UITableView *tableView;

@property (nonatomic,
retain) NSMutableArray *allDataArray;

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

[self
addAllViews];

[self
loadAllData];

}

#pragma mark 添加全部页面元素

- (void)addAllViews

{

self.tableView = [[UITableView
alloc] initWithFrame:[UIScreen
mainScreen].bounds
style:UITableViewStylePlain];

_tableView.delegate =
self;

_tableView.dataSource =
self;

[self.view
addSubview:_tableView];

#warning iOS8 -
分割线样式

UIBlurEffect *blurEffect = [UIBlurEffect
effectWithStyle:UIBlurEffectStyleDark];

UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect
effectForBlurEffect:blurEffect];

_tableView.separatorEffect = blurEffect;

//
注册

[_tableView
registerClass:[UITableViewCell
class] forCellReuseIdentifier:@"cellIdentifier"];

//
右上角编辑按钮

self.navigationItem.rightBarButtonItem =
self.editButtonItem;

}

#pragma mark 加载所有数据

- (void)loadAllData

{

self.allDataArray =[[NSMutableArray
alloc]
initWithObjects:@"王菲",@"周迅",@"李冰冰",@"白冰",@"紫薇",@"马苏",@"刘诗诗",@"赵薇",@"angelbaby",@"熊黛林",nil];

}

#pragma mark 编辑按钮

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

[super
setEditing:editing animated:animated];

[_tableView
setEditing:!_tableView.isEditing
animated:YES];

}

#pragma mark - UITableViewDataSource Methods

#pragma mark 设置有多少分组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return
1;

}

#pragma mark 设置每个分组有多少行

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

{

return
_allDataArray.count;

}

#pragma mark 设置某行上显示的内容

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

{

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"cellIdentifier"
forIndexPath:indexPath];

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

//   
cell.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 256.0
green:arc4random() % 256 / 256.0 blue:arc4random() % 256 / 256.0 alpha:1.0f];

return cell;

}

#pragma mark 设置可以进行编辑

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

{

return
YES;

}

#pragma mark 设置编辑的样式

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

{

return
UITableViewCellEditingStyleDelete;

}

#pragma mark 设置处理编辑情况

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

{

if (editingStyle ==
UITableViewCellEditingStyleDelete) {

// 1.
更新数据

[_allDataArray
removeObjectAtIndex:indexPath.row];

// 2.
更新UI

[tableView
deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

}

}

#pragma mark 设置可以移动

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

{

return
YES;

}

#pragma mark 处理移动的情况

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath

{

// 1. 更新数据

NSString *title =
_allDataArray[sourceIndexPath.row];

[_allDataArray
removeObject:title];

[_allDataArray
insertObject:title atIndex:destinationIndexPath.row];

// 2. 更新UI

[tableView moveRowAtIndexPath:sourceIndexPath
toIndexPath:destinationIndexPath];

}

/**

*  tableView:editActionsForRowAtIndexPath:     //
设置滑动删除时显示多个按钮

*  UITableViewRowAction                        //
通过此类创建按钮

*  1. 我们在使用一些应用的时候,在滑动一些联系人的某一行的时候,会出现删除、置顶、更多等等的按钮,在iOS8之前,我们都需要自己去实现。But,到了iOS8,系统已经写好了,只需要一个代理方法和一个类就搞定了

*  2. iOS8的<UITableViewDelegate>协议多了一个方法,返回值是数组的tableView:editActionsForRowAtIndexPath:方法,我们可以在方法内部写好几个按钮,然后放到数组中返回,那些按钮的类就是UITableViewRowAction

*  3. 在UITableViewRowAction类,我们可以设置按钮的样式、显示的文字、背景色、和按钮的事件(事件在Block中实现)

*  4. 在代理方法中,我们可以创建多个按钮放到数组中返回,最先放入数组的按钮显示在最右侧,最后放入的显示在最左侧

*  5. 注意:如果我们自己设定了一个或多个按钮,系统自带的删除按钮就消失了...

*/

#warning iOS8 -

#pragma mark 在滑动手势删除某一行的时候,显示出更多的按钮

- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath
*)indexPath

{

// 添加一个删除按钮

UITableViewRowAction *deleteRowAction = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleDestructive
title:@"删除"
handler:^(UITableViewRowAction *action,
NSIndexPath *indexPath) {

NSLog(@"点击了删除");

// 1. 更新数据

[_allDataArray
removeObjectAtIndex:indexPath.row];

// 2. 更新UI

[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];

}];

//    // 删除一个置顶按钮

//    UITableViewRowAction *topRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"置顶" handler:^(UITableViewRowAction
*action, NSIndexPath *indexPath) {

//        NSLog(@"点击了置顶");

//

//        // 1.
更新数据

//        [_allDataArray exchangeObjectAtIndex:indexPath.row withObjectAtIndex:0];

//

//        // 2.
更新UI

//        NSIndexPath *firstIndexPath = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];

//        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];

//    }];

//    topRowAction.backgroundColor = [UIColor blueColor];

//
添加一个更多按钮

UITableViewRowAction *moreRowAction = [UITableViewRowAction
rowActionWithStyle:UITableViewRowActionStyleNormal
title:@"更多"
handler:^(UITableViewRowAction *action,
NSIndexPath *indexPath) {

NSLog(@"点击了更多");

[tableView
reloadRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationMiddle];

}];

moreRowAction.backgroundEffect = [UIBlurEffect
effectWithStyle:UIBlurEffectStyleDark];

//
将设置好的按钮放到数组中返回

// return @[deleteRowAction, topRowAction, moreRowAction];

return
@[deleteRowAction,moreRowAction];

}

自定义cell右侧 多按钮的更多相关文章

  1. IOS 中关于自定义Cell 上的按钮 开关等点击事件的实现方法(代理)

    1.在自定义的Cell .h文件中写出代理,写出代理方法. @protocol selectButtonDelegate <NSObject> -(void)selectModelID:( ...

  2. iOS tableview自定义cell上添加按钮实现删除功能

    在删除的时候,先删除数据源,再删除cell 但是,会发现一直崩: numberOfRowsInSection 解决方案:

  3. iOS 获取自定义cell上按钮所对应cell的indexPath.row的方法

    在UITableView或UICollectionView的自定义cell中创建一button,在点击该按钮时知道该按钮所在的cell在UITableView或UICollectionView中的行数 ...

  4. iOS深入学习(UITableView系列4:使用xib自定义cell)

    可以通过继承UITableViewCell重新自定义cell,可以像下面一样通过代码来自定义cell,但是手写代码总是很浪费时间, ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  5. 08-UIKit(UITableTableViewCell、自定义Cell、xcode调试)

    目录: 1. UITableTableViewCell 2. tag技术 3. 自定义Cell 4. 用nib文件构造自定义的静态表 5. TableView数据模型总结 6. Xcode代码调试 & ...

  6. IOS开发中UITableView(表视图)的滚动优化及自定义Cell

    IOS开发中UITableView(表视图)的滚动优化及自定义Cell IOS 开发中UITableView是非常常用的一个控件,我们平时在手机上看到的联系人列表,微信好友列表等都是通过UITable ...

  7. React Native商城项目实战09 - 个人中心自定义cell

    1.新建组件CommonMyCell.js /** * 个人中心自定义cell */ import React, { Component } from 'react'; import { AppReg ...

  8. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  9. 自定义cell自适应高度

    UITableView在许多App种被大量的应用着,呈现出现的效果也是多种多样的,不能局限于系统的一种样式,所以需要自定义cell 自定义cell呈现的内容也是多种多样的,内容有多有少,所以需要一种能 ...

随机推荐

  1. ghoest32 不重启电脑手动备份系统为.gho

    备份系统我们一般使用DOS之家的ghoest备份工具,但备份必须是重启电脑在DOS命令行下,其实,可以不重启电脑备份系统,也就是手动备份系统.DOS之家用的ghoest本质也是赛门铁克公司出的ghoe ...

  2. jsf组件对应表

    组件 标签 说明 UIForm form 表示 HTML表单元素 UIInput InputText 单行文本输入控件   inputTextarea 多行文本输入控件   InputSecret 密 ...

  3. Web GIS离线解决方案

    1.背景 在离线环境下(局域网中)的GIS系统中如何使用地图?这里的地图主要指的是地图底图,有了底图切片数据,我们就可以看到地图,在上面加上自己的业务数据图层,进行相关操作. 要在离线环境下看到GIS ...

  4. Dapper完美兼容Oracle,执行存储过程,并返回结果集。

    Dapper完美兼容Oracle,执行存储过程,并返回结果集. 这个问题,困扰了我整整两天. 刚刚用到Dapper的时候,感觉非常牛掰.特别是配合.net 4.0新特性dynamic,让我生成泛型集合 ...

  5. java网络编程2

    在通信双方中,ServerSocket是服务器端负责接收的一方,它负责监听指定端口,其构造函数如下: 1.ServerSocket()  throws IOException;无参构造函数,之所以存在 ...

  6. AsyncTask异步上传文本到服务器

    服务器代码:用于接收客户端信息 package ches; import java.io.IOException; import java.io.PrintWriter; import javax.s ...

  7. div内文字超出换行问题

    1.强制换行: div的样式加上: word-wrap:break-word;word-break:break-all; 2.在文字中间加入建议换行标志<wbr>可以每隔几个字符加一个,

  8. CentOS6.5的vsftp搭建流程(一)

    前几次搭建FTP都失败了,不是登陆不了,就是目录没有权限.现在终于摸索出了靠谱的操作流程,分享之~ 1. 查看是否安装了vsftpd,未安装则安装 [root@iZ283tian2dZ /]# rpm ...

  9. ASP.NET下回车键的触发效果

    在ASP.NET下,在客户端触发回车键,默认调用了页面中第一个button,这有时是非常头痛的,比如页面的第一个按键是注销键时,想想也够可怕了. .net提供设置默认回车键的属性,this.Form. ...

  10. EF DbContext 并发执行时可能出现的问题

    现在许多Web项目都使用了IOC的DI注入组件.其中对象的生命周期管理是非常重要的. 有时我们为了提高请求的响应,经常在请求线程中执行多个子线程,然而忽略了EF的DbContext的生命周期管理. D ...