iOS个人整理19-UITableViewController和UITableView的编辑

2024-05-11 21:32

本文主要是介绍iOS个人整理19-UITableViewController和UITableView的编辑,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、UITableViewController

UITableViewController是继承于UIViewController中的一个类,只不过比UIViewController中多了一个属性tableView。

也就是说UITableViewController是自带table的视图控制器。


它的self.view 是UITableView而不是UIView。

dataSource和delegate都默认是self。


总而言之,省了不少事情,相当于创建了一个UIViewController并在上面声明了完整的UITableView。



二、UITableView的编辑


对于UITableView来说,经常需要删除或者添加一条信息,比如,通讯录中的滑动删除和添加信息。

简单来说就是对单元格进行删除,添加,插入,以及调换顺序的操作。

这些操作主要通过各种代理方法来实现

1.让tableView处于编辑状态

//系统提供的方法按钮会调用此方法,让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{//设置父类进入编辑状态[super setEditing:editing animated:animated];//开启表视图的编辑状态UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView setEditing:editing animated:animated];}//确定cell是否可以进入编辑状态,为NO就不能进入编辑状态
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}

2.设置编辑状态的style,决定插入或删除

//设置编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//如果是最后一行,编辑模式为插入if (indexPath.row == _allDataMutableArray.count-1) {return UITableViewCellEditingStyleInsert;}elsereturn UITableViewCellEditingStyleDelete;
}

3.编辑状态的提交,对tableView进行改变

//编辑状态的提交
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{UITableView *tempTableView = [self.view viewWithTag:1000];//如果编辑模式为删除if (editingStyle == UITableViewCellEditingStyleDelete) {//先删除数据源,也就是数组中的内容[_allDataMutableArray removeObjectAtIndex:indexPath.row];//再删除单元格//这里要特别注意,必须先操作数据再操作单元格,不然崩的不要不要的[tempTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];}//如果编辑模式为插入if (editingStyle == UITableViewCellEditingStyleInsert) {//先插入到数组[_allDataMutableArray insertObject:@"新人" atIndex:indexPath.row];//再创建单元格[tempTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
}


下面是移动单元格的方法

1.首先还是要进入编辑状态,与上面第一步相同

2.设置是否可以移动

//实现协议,告诉tableView是否能移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}

3.检测移动的过程,可以对移动的源位置和目的位置进行限制

//监测移动过程,限制最后一行不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{//设置,如果要移动最后一行或者移动到最后一行,是不允许的if (sourceIndexPath.row == _allDataMutableArray.count - 1 || proposedDestinationIndexPath.row == _allDataMutableArray.count - 1) {return sourceIndexPath;}elsereturn proposedDestinationIndexPath;
}

4.进行移动的数据操作,如果不添加这一步,就没有修改数据源,重新加载后会复原

//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//先得到单元格上的数据NSString *tempStr = [self.allDataMutableArray objectAtIndex:sourceIndexPath.row];//把原位置的数据删除[_allDataMutableArray removeObjectAtIndex:sourceIndexPath.row];//把新数据添加到数组中对应的位置[_allDataMutableArray insertObject:tempStr atIndex:destinationIndexPath.row];}

5.让tableView重新加载数据

//刷新数据
-(void)refreshAction:(UIBarButtonItem*)sender
{UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView reloadData];
}



这里再贴上完整的代码和效果

//
#import "RootViewController.h"@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>@property (nonatomic,retain)NSMutableArray* allDataMutableArray;@end@implementation RootViewController- (void)viewDidLoad {[super viewDidLoad];//标题self.navigationItem.title = @"表视图的编辑";//添加右侧按钮//添加按钮UIBarButtonItem* addBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction:)];//编辑按钮UIBarButtonItem* editBarButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(editAction:)];//添加到右侧self.navigationItem.rightBarButtonItems = @[self.editButtonItem,addBarButton,editBarButton];//添加左侧刷新按钮self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshAction:)];//初始化UITableView *myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];myTableView.tag = 1000;//代理myTableView.delegate = self;myTableView.dataSource = self;//添加到父视图[self.view addSubview:myTableView];//显示相关的属性//行高myTableView.rowHeight = 70;//分割线myTableView.separatorColor = [UIColor blueColor];myTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;//置顶视图myTableView.tableHeaderView = [[UIView alloc]init];//置底视图myTableView.tableFooterView = [[UIView alloc]init];//初始化数组数据self.allDataMutableArray = [[NSMutableArray alloc]initWithObjects:@"张三",@"李四",@"我擦",@"123",@"",@"111",@"333", nil];}//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return _allDataMutableArray.count;
}//cell填充
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//cell重用机制//定义重用标示符static NSString* cellId = @"CELL";//每次需要使用单元格的是,先根据重用标识符从重用队列中获取单元格,如果队列中没有,再进行初始化新的单元格//每次都会先创建一屏幕的cell,当有cell出屏幕,就会根据重用标识符添加到对应的重用队列中,当屏幕外的cell要进入屏幕,先从队列中获取,如果没有,则初始化cell//当重用cell时,需要对上面的控件程序赋值UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];//如果从重用队列中未获取cell,也就是Cell为空if (!cell) {cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];//系统提供的控件要配合不同的样式来使用
//        cell.detailTextLabel.text = [NSString stringWithFormat:@"创建的第%d个单元格",self.index++];}//UITableViewCell的属性//选中效果cell.selectionStyle = UITableViewCellSelectionStyleBlue;//辅助视图的样式cell.accessoryType = UITableViewCellAccessoryCheckmark;//设置左侧图片cell.imageView.image = [UIImage imageNamed:@"ck.jpg"];//标题视图cell.detailTextLabel.text = [NSString stringWithFormat:@"第%ld个cell,%ld个section",indexPath.row,indexPath.section];//副标题视图cell.textLabel.text = _allDataMutableArray[indexPath.row];//为相应位置返回定制好的单元格return cell;}
#pragma mark -- 按钮触发方法
//点击添加触发的方法
-(void)addAction:(UIBarButtonItem*)sender
{}
//进入编辑状态
-(void)editAction:(UIBarButtonItem*)sender
{[self setEditing:YES animated:NO];
}
//刷新数据
-(void)refreshAction:(UIBarButtonItem*)sender
{UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView reloadData];
}#pragma mark -- 编辑相关代理方法//系统提供的方法按钮会调用此方法,让tableView处于编辑状态
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{[super setEditing:editing animated:animated];//开启表视图的编辑状态UITableView *tempTableView = [self.view viewWithTag:1000];[tempTableView setEditing:editing animated:animated];}
//协议设定
//确定cell是否处于编辑状态,为NO就不能进入编辑状态
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}//编辑状态的提交
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{UITableView *tempTableView = [self.view viewWithTag:1000];//如果编辑模式为删除if (editingStyle == UITableViewCellEditingStyleDelete) {//先删除数据源,也就是数组中的内容[_allDataMutableArray removeObjectAtIndex:indexPath.row];//再删除单元格//这里要特别注意,必须先操作数据再操作单元格,不然崩的不要不要的[tempTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];}//如果编辑模式为插入if (editingStyle == UITableViewCellEditingStyleInsert) {//先插入到数组[_allDataMutableArray insertObject:@"新人" atIndex:indexPath.row];//再创建单元格[tempTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
}//设置编辑的样式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//如果是最后一行,编辑模式为插入if (indexPath.row == _allDataMutableArray.count-1) {return UITableViewCellEditingStyleInsert;}elsereturn UITableViewCellEditingStyleDelete;
}//移动相关
//实现协议,告诉tableView是否能移动
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{return YES;
}//移动
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{//先得到单元格上的数据NSString *tempStr = [self.allDataMutableArray objectAtIndex:sourceIndexPath.row];//把原位置的数据删除[_allDataMutableArray removeObjectAtIndex:sourceIndexPath.row];//把新数据添加到数组中对应的位置[_allDataMutableArray insertObject:tempStr atIndex:destinationIndexPath.row];}//监测移动过程,限制最后一行不能移动
-(NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{if (sourceIndexPath.row == _allDataMutableArray.count - 1 || proposedDestinationIndexPath.row == _allDataMutableArray.count - 1) {return sourceIndexPath;}elsereturn proposedDestinationIndexPath;
}@end

效果如下

 



这篇关于iOS个人整理19-UITableViewController和UITableView的编辑的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/980706

相关文章

Python变量与数据类型全解析(最新整理)

《Python变量与数据类型全解析(最新整理)》文章介绍Python变量作为数据载体,命名需遵循字母数字下划线规则,不可数字开头,大小写敏感,避免关键字,本文给大家介绍Python变量与数据类型全解析... 目录1、变量变量命名规范python数据类型1、基本数据类型数值类型(Number):布尔类型(bo

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)

《MySQL复杂SQL之多表联查/子查询详细介绍(最新整理)》掌握多表联查(INNERJOIN,LEFTJOIN,RIGHTJOIN,FULLJOIN)和子查询(标量、列、行、表子查询、相关/非相关、... 目录第一部分:多表联查 (JOIN Operations)1. 连接的类型 (JOIN Types)

JAVA数组中五种常见排序方法整理汇总

《JAVA数组中五种常见排序方法整理汇总》本文给大家分享五种常用的Java数组排序方法整理,每种方法结合示例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录前言:法一:Arrays.sort()法二:冒泡排序法三:选择排序法四:反转排序法五:直接插入排序前言:几种常用的Java数组排序

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Python+Tkinter实现Windows Hosts文件编辑管理工具

《Python+Tkinter实现WindowsHosts文件编辑管理工具》在日常开发和网络调试或科学上网场景中,Hosts文件修改是每个开发者都绕不开的必修课,本文将完整解析一个基于Python... 目录一、前言:为什么我们需要专业的Hosts管理工具二、工具核心功能全景图2.1 基础功能模块2.2 进

Mysql中深分页的五种常用方法整理

《Mysql中深分页的五种常用方法整理》在数据量非常大的情况下,深分页查询则变得很常见,这篇文章为大家整理了5个常用的方法,文中的示例代码讲解详细,大家可以根据自己的需求进行选择... 目录方案一:延迟关联 (Deferred Join)方案二:有序唯一键分页 (Cursor-based Paginatio

使用DeepSeek搭建个人知识库(在笔记本电脑上)

《使用DeepSeek搭建个人知识库(在笔记本电脑上)》本文介绍了如何在笔记本电脑上使用DeepSeek和开源工具搭建个人知识库,通过安装DeepSeek和RAGFlow,并使用CherryStudi... 目录部署环境软件清单安装DeepSeek安装Cherry Studio安装RAGFlow设置知识库总

Mysql中InnoDB与MyISAM索引差异详解(最新整理)

《Mysql中InnoDB与MyISAM索引差异详解(最新整理)》InnoDB和MyISAM在索引实现和特性上有差异,包括聚集索引、非聚集索引、事务支持、并发控制、覆盖索引、主键约束、外键支持和物理存... 目录1. 索引类型与数据存储方式InnoDBMyISAM2. 事务与并发控制InnoDBMyISAM