android tableview实现多选功能,iOS开发UI篇-tableView在编辑状态下的批量操作(多选)...

这篇博客介绍了如何在iOS应用中实现UITableView的选择和编辑功能。通过设置数据源、添加删除行的编辑样式,以及处理选择和取消选择的逻辑,实现了用户交互。此外,还展示了如何在代码中切换编辑状态以及全选表格内容的方法。
摘要由CSDN通过智能技术生成

先看下效果图

7333475072d6d29306bc31f164607600.png

直接上代码

#import "MyController.h"

@interface MyController ()

{

UIButton *button;

}

@property(nonatomic,strong)NSMutableArray *array;//数据源

@property (nonatomic,strong)NSMutableArray *selectorPatnArray;//存放选中数据

@end

@implementation MyController

- (void)viewDidLoad {

[super viewDidLoad];

//添加数据源

for (int i = ; i < ; i++) {

NSString *str = [NSString stringWithFormat:@"第%d行",i + ];

[self.array addObject:str];

}

button = [UIButton buttonWithType:(UIButtonTypeCustom)];

[button setTitle:@"选择" forState:(UIControlStateNormal)];

[button setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];

button.frame = CGRectMake(, , , );

[button addTarget:self action:@selector(selectMore:) forControlEvents:(UIControlEventTouchUpInside)];

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

return ;

}

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

return self.array.count;

}

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

static NSString *Identifier = @"myCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];

if (!cell) {

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

}

cell.textLabel.text = self.array[indexPath.row];

cell的selectionStyle不要设置为UITableViewSelectionStyleNone

return cell;

}

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

{

return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

//选中数据

[self.selectorPatnArray addObject:self.array[indexPath.row]];

}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{

//从选中中取消

if (self.selectorPatnArray.count > ) {

[self.selectorPatnArray removeObject:self.array[indexPath.row]];

}

}

#pragma mark - 点击事件

- (void)selectMore:(UIBarButtonItem *)action{

if ([button.titleLabel.text isEqualToString:@"选择"]) {

//移除之前选中的内容

if (self.selectorPatnArray.count > ) {

[self.selectorPatnArray removeAllObjects];

}

[button setTitle:@"确认" forState:(UIControlStateNormal)];

//进入编辑状态

[self.tableView setEditing:YES animated:YES];

}else{

[button setTitle:@"选择" forState:(UIControlStateNormal)];

//对选中内容进行操作

NSLog(@"选中个数是 : %lu 内容为 : %@",(unsigned long)self.selectorPatnArray.count,self.selectorPatnArray);

//取消编辑状态

[self.tableView setEditing:NO animated:YES];

}

}

#pragma mark -懒加载

-(NSMutableArray *)array{

if (!_array) {

_array = [NSMutableArray array];

}

return _array;

}

- (NSMutableArray *)selectorPatnArray{

if (!_selectorPatnArray) {

_selectorPatnArray = [NSMutableArray array];

}

return _selectorPatnArray;

}

如果要把tableView在非编辑状态下不让点击,设置下这个属性,就OK了.

@property (nonatomic) BOOL allowsSelection NS_AVAILABLE_IOS(3_0); // default is YES. Controls whether rows can be selected when not in editing mode

如果在某些情况下需要全选,可以按照这个思路:

for (int i = ; i < self.array.count; i++) {

NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:];

UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:path];

cell.selected = YES;

[self.selectorPatnArray addObject:self.array[i]];//添加到选中列表

}

这只是个人想法,欢迎指出不足......

iOS开发UI篇—UITableview控件基本使用

iOS开发UI篇—UITableview控件基本使用 一.一个简单的英雄展示程序 NJHero.h文件代码(字典转模型) #import ...

iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

iOS开发UI篇—核心动画&lpar;UIView封装动画&rpar;

iOS开发UI篇—核心动画(UIView封装动画) 一.UIView动画(首尾) 1.简单说明 UIKit直接将动画集成到UIView类中,当内部的一些属性发生改变时,UIView将为这些改变提供动画 ...

iOS开发UI篇—核心动画&lpar;关键帧动画&rpar;

转自:http://www.cnblogs.com/wendingding/p/3801330.html iOS开发UI篇—核心动画(关键帧动画) 一.简单介绍 是CApropertyAnimatio ...

iOS开发UI篇—核心动画&lpar;基础动画&rpar;

转自:http://www.cnblogs.com/wendingding/p/3801157.html 文顶顶 最怕你一生碌碌无为 还安慰自己平凡可贵 iOS开发UI篇—核心动画(基础动画) iOS ...

iOS开发UI篇—核心动画简介

转自:http://www.cnblogs.com/wendingding/p/3801036.html iOS开发UI篇—核心动画简介 一.简单介绍 Core Animation,中文翻译为核心动画 ...

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布 ...

iOS开发UI篇—Quartz2D使用(绘制基本图形)

iOS开发UI篇—Quartz2D使用(绘制基本图形) 一.简单说明 图形上下文(Graphics Context):是一个CGContextRef类型的数据 图形上下文的作用:保存绘图信息.绘图状态 ...

iOS开发UI篇—Button基础

iOS开发UI篇—Button基础 一.简单说明 一般情况下,点击某个控件后,会做出相应反应的都是按钮 按钮的功能比较多,既能显示文字,又能显示图片,还能随时调整内部图片和文字的位置 二.按钮的三种状 ...

随机推荐

Empire C:Basic 4

一.变量名 1.名字由字母和数字组成,但其第一个字符必须为字母. 2.变量名不要以下划线开头. 3.变量名使用小写字母,符号常量名全部使用大写字母. 二.数据类型及长度 1.char 字符型 占用一个 ...

WPF异步调用WCF服务

wpf调用wcf时,第一次访问总耗时到达几秒,影响界面的用户体验,因此在wpf加载界面和加载数据时采用异步加载,即异步访问wcf服务, 由于是否采用异步加载和服务端无关,仅仅由客户端自己根据需要来选择 ...

C&num;获取QQ旋风的下载记录

/* * 用户:从前的我 * 日期:2015/8/26 */ using System; using System.IO; namespace GetXf { class Program { publ ...

progressBar的使用

<?xml version="1.0" encoding="utf-8"?>

python 中 list 的各项操作

最近在学习 python 语言.大致学习了 python 的基础语法.觉得 python 在数据处理中的地位和它的 list 操作密不可分. 特学习了相关的基础操作并在这里做下笔记. ''' Pyth ...

less的安装和使用

资料: https://www.cnblogs.com/starof/p/5226739.html

a 标签实现分享功能

在网页中,经常会用到分享功能,例如分享到qq,分享到微信,分享到微博等,但是怎么实现呢?一直没有想清楚这个问题,觉得好高大上的样子,于是在网上找了一些资料,也没有看出个什么所以然来: 于是有些心急了, ...

转载:实现MATLAB2016a和M文件关联

转载自http://blog.csdn.net/qq_22186119 新安装MATLAB2016a之后,发现MATLAB没有和m文件关联 每次打开m文件后都会重新打开一次MATLAB主程序 后来发现 ...

ssm的架构及整合说明

SSM,即 SpringMVC.Spring 与 MyBatis 三个框架 它们在三层架构中所处的位置是不同的,即它们在三层架构中的功能各不相同,各司其职 SpringMVC:作为 View 层的实现 ...

HTML5 Canvas &lpar; 图形变换矩阵 &rpar; transform&comma; setTransform

...
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值