UITableView显示数据,增加数据,删除数据及移动数据行
UITableView和html中的table有点类似的,也有header和footer和body,row。下面给出一个demo
//
// TableViewTestViewController.m
// iosstudy2024
//
// Created by figo on 2024/12/9.
//#import "TableViewTestViewController.h"@interface TableViewTestViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableViewTest;
@property(strong,nonatomic) NSMutableArray *data;- (IBAction)addData:(id)sender;
- (IBAction)deleteData:(id)sender;@end@implementation TableViewTestViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view from its nib.self.tableViewTest.dataSource=self;self.tableViewTest.delegate=self;NSArray *array=@[@"iphone",@"华为",@"小米",@"oppo",@"vivo"@"iphone",@"华为",@"小米",@"oppo",@"vivo",@"iphone",@"华为",@"小米",@"oppo",@"vivo"@"iphone",@"华为",@"小米",@"oppo",@"vivo"];self.data=[NSMutableArray arrayWithArray:array];
}/*
#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {// Get the new view controller using [segue destinationViewController].// Pass the selected object to the new view controller.
}
*///tableViewCell 表格每一行
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {// UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"abc"];UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"abc"];if(cell==nil){cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"abc"];cell.imageView.image=[UIImage imageNamed:@"diamond"];cell.textLabel.text=@"AAAAA";cell.detailTextLabel.text= self.data[indexPath.row];}return cell;
}
//每段总行数
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.data.count;
}
//总段数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{return 1;
}
//头部标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{return @"头部视图";
}
//尾部标题
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{return @"尾部视图";
}
//头部返回视图 会覆盖titleForHeaderInSection
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{int width=[UIScreen mainScreen].bounds.size.width;UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];imgView.image=[UIImage imageNamed:@"star"];UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 200, 50)];label.text=@"这里是头部标题";UIView *view=[[UIView alloc]init];view.frame=CGRectMake(0, 0, width, 100);view.backgroundColor=[UIColor systemPinkColor];[view addSubview:imgView];[view addSubview:label];return view;
}
//尾部返回视图 会覆盖titleForFooterInSection
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{int width=[UIScreen mainScreen].bounds.size.width;UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50,50)];imgView.image=[UIImage imageNamed:@"diamond"];UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(100, 0, 200, 50)];label.text=@"这里是尾部标题";UIView *view=[[UIView alloc]init];view.frame=CGRectMake(0, 0, width, 100);view.backgroundColor=[UIColor purpleColor];[view addSubview:imgView];[view addSubview:label];return view;
}
//选中一行弹框效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{NSString *str=self.data[indexPath.row];//创建一个UIAlertController对象//P1:弹出框的标题 P2弹出框的内容//P3:弹出的警告框的样式为UIAlertControllerStyleAlert(即中心弹出的警告框)UIAlertController* alertController = [UIAlertController alertControllerWithTitle:@"标题" message:str preferredStyle:UIAlertControllerStyleAlert];//添加“确认”动作按钮到控制器上//P1:标题文字 P2:动作样式,有三种动作样式:常规(default)、取消(cancel)以及警示(destruective)//P3:用户选中并点击该动作时,所执行的代码UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {// 用户点击确认按钮后执行的代码}];//将动作按钮添加到alertController视图上[alertController addAction:defaultAction];//添加“取消”动作按钮到控制器上UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {// 用户点击取消按钮后执行的代码}];//将动作按钮添加到alertController视图上[alertController addAction:cancelAction];//将警告框显示出来[self presentViewController:alertController animated:YES completion:nil];}
//每行能否编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{return YES;
}
//提交编辑(增删)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{if(editingStyle==UITableViewCellEditingStyleDelete){//删除数据源,这个必须排在前面[self.data removeObjectAtIndex:indexPath.row];//删除当前行 记得加上@ indexPath是个对象,UITableViewRowAnimationLeft表示从左移动[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}else if (editingStyle==UITableViewCellEditingStyleInsert){//先增加数据源[self.data insertObject:@"这里是新增的行" atIndex:indexPath.row];//视图增加一行[tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];}
}
//设置删除按钮的文字
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{return @"删除";
}//设置tableView不可以编辑
- (IBAction)deleteData:(id)sender {self.tableViewTest.editing=NO;}
//设置tableView可以编辑
- (IBAction)addData:(id)sender {self.tableViewTest.editing=YES;
}
//tableViewTest.editing=YES默认是删除,改成编辑
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{if(self.tableViewTest.editing==YES){return UITableViewCellEditingStyleInsert;}else{return UITableViewCellEditingStyleDelete;}
}//是否可以移动每一行
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath{return YES;
}- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{//1.需要移动的行的数据NSString *celldata=self.data[sourceIndexPath.row];//2.移除原始位置的数据[self.data removeObjectAtIndex:sourceIndexPath.row];//3.将原始位置的数据移动到目标位置[self.data insertObject:celldata atIndex:destinationIndexPath.row];
}@end
相关文章:

UITableView显示数据,增加数据,删除数据及移动数据行
UITableView和html中的table有点类似的,也有header和footer和body,row。下面给出一个demo // // TableViewTestViewController.m // iosstudy2024 // // Created by figo on 2024/12/9. //#import "TableViewTestViewController.h"interfa…...

金智塔科技喜获CCF中国数字金融大会 GraphRAG竞赛二等奖
12月7日,CCF 首届中国数字金融大会GraphRAG竞赛在上海落下帷幕,金智塔科技(团队名称:塔塔向前冲)从众多参赛队伍中脱颖而出,喜获二等奖。 CCF 首届中国数字金融大会由中国计算机学会主办,中国计…...

方案解读:数字化扩展中如何提升多云应用安全能力?
越来越多企业选择上云,拥抱数字化转型。数据显示,在过去一年中,将应用托管至六种不同环境中的企业比例已经翻倍,达到令人震惊的38%。与此同时,应用和流经其的关键数据已成为日益复杂的网络攻击的首选目标,且…...

“年轻科技旗舰”爱玛A7 Plus正式发布,全国售价4999元
12月18日,备受行业瞩目的“A7上场 一路超神”爱玛旗舰新品发布会在爱玛台州智造工厂盛大举行。 作为年末“压轴产品”的“两轮豪华轿跑”爱玛A7Plus重磅上场,以“快、稳、帅、炫、智、爽”六大超神技惊艳四座,不仅践行了爱玛科技的精品战略&…...

oracle开窗函数笔记、over()笔记
文章目录 开窗函数、组函数、分析函数概念聚合函数和分析函数的区别partition by后面也可以跟多个字段 开窗函数一定要加 聚合函数、或分析函数吗,否则会报错lag()和lead()的用法lag和lead实战开窗函数可以和其他函数一起使用吗? TODO开窗函数中的count(1)是什么意…...

【HarmonyOS】HarmonyOS 和 Flutter混合开发 (一)之鸿蒙Flutter环境安装
【HarmonyOS】HarmonyOS 和 Flutter混合开发 (一)之鸿蒙Flutter环境安装 一、前言 flutter作为开源适配框架方案,已经在Android,IOS,Web,Window四大平台进行了适配,一套代码,可以同…...

海外招聘丨卢森堡大学—人工智能和机器学习中的 PI 用于图像分析
雇主简介 卢森堡大学立志成为欧洲最受推崇的大学之一,具有鲜明的国际化、多语言和跨学科特色。 她促进研究和教学的相互影响,与国家息息相关,因其在特定领域的研究和教学而闻名于世,并成为当代欧洲高等教育的创新典范。 她的核…...

LeetCode hot100-85
https://leetcode.cn/problems/coin-change/?envTypestudy-plan-v2&envIdtop-100-liked 322. 零钱兑换 已解答 中等 相关标签 相关企业 给你一个整数数组 coins ,表示不同面额的硬币;以及一个整数 amount ,表示总金额。计算并返回可以凑…...

linux 内核数据包处理中的一些坑和建议
1、获取IP头部 iph ip_hdr(skb); struct sk_buff { ...... sk_buff_data_t transport_header; /* Transport layer header */ sk_buff_data_t network_header; /* Network layer header */ sk_buff_data_t mac_header; /* Link layer header */ ...... } 1࿰…...

C++ 的衰退复制(decay-copy)
目录 1.什么是衰退复制(decay-copy) 1.1.推导规则 1.2.LWG issue 929 1.3.想象中的 decay_copy 2.decay-copy 与 auto 2.1.为什么引入衰退复制 2.2. 成为 C 23 的语言特性 3.应用场景 4.总结 1.什么是衰退复制(decay-copy࿰…...

vue-cli 5接入模块联邦 module federation
vue-cli 5接入模块联邦 module federation 模块联邦概念实现思路配置遇到的问题: 模块联邦概念 模块联邦由webpack 5最先推出的,让应用加载远程的代码模块来实现不同的Web应用共享代码片段.模块联邦分为两个角色,一个是生产者,一个是消费者.生产者暴露代码供消费者消费 (用一个…...

【Rust自学】3.6. 控制流:循环
3.6.0. 写在正文之前 欢迎来到Rust自学的第三章,一共有6个小节,分别是: 变量与可变性数据类型:标量类型数据类型:复合类型函数和注释控制流:if else控制流:循环(本文) 通过第二章…...

【第八节】git与github
目录 前言 一、 远程仓库概述 二、 创建、配置、连接推送远程仓库 2.1 在 GitHub 上创建仓库 2.2 生成 SSH Key 2.3 验证 SSH 连接 2.4 本地初始化仓库 2.5 推送本地仓库到远程 三、 管理远程仓库 3.1 查看远程仓库 3.2 提取远程仓库更新 3.3 推送更新到远程仓库 …...

win如何访问Linux数据库(本地)
对于数据库的学习,我们都是在localhost主机上进行操作,当我们在Linux系统上安装数据库时,我们就有了尝试在win上去访问Linux上的数据库的想法。 数据库中的用户: 我们都知道数据库中顶级的用户为root,在做创建用户的联…...

Windows设置所有软件默认以管理员身份运行
方法一、修改注册表 winr打开运行,输入“regedit”打开注册表; 打开此路径“计算机HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem”; 在右侧找到“EnableLUA”,将其值改为0,重启电脑。 …...

前端 计算发布时间(如“1小时前”、“3天前”等)
这样效果,在c端比较常见,通过前端也可以处理 代码如下: // 计算 小时timeAgo(createTime) {// 将 createTime 字符串转换为 Date 对象 const createDate new Date(createTime);const now new Date();const diffInSeconds Math.floor((now…...

shardingjdbc 4.0.0 seata分布式事务Failed to fetch schema问题
报错 12-18 15:18:35.931 [ERROR] [i.s.r.d.s.s.cache.AbstractTableMetaCache:63 ] [traceId:][spanId:] - get table meta of the table wh_stock_log error: Failed to fetch schema of xxx java.sql.SQLException: Failed to fetch schema of wh_stock_logat io.seata.r…...

罗德与施瓦茨NRT2功率反射仪,NRT2通过式功率计
罗德与施瓦茨NRT2功率反射仪NRT2 通过式功率计 描述 定向/通过式功率传感器在线测量正向和反向功率。在安装、维修和监控发射机、天线和射频发生器时,需要进行这些测量。R&SNRT系列由R&SNRT2功率反射计及各种R&SNRT Zxx定向功率传感器。 由于其测量功…...

QLineEdit限制输入固定字节数(UTF-8编码)
setMaxLength(int)只能用来限制输入的字符个数 QLineEdit *editor new QLineEdit(parent); editor->setMaxLength(32); 1、如果是单字节字符,如数字,字母等,字符数正好等于字节数 2、如果是多字节字符,UTF8编码时࿰…...

基于ubuntu的mysql 8.0安装教程
文章目录 1.查看版本2.切换到root账户3.下载安装包4.问题的解决5.查看是否解压成功6.安装我们的发布包7.更新包的内容8.下载mysql9.查看mysql的状态10.设置开机自启动11.登录mysql 公司里面的mysql根本不会出现在windows操作系统上面,下面我们演示的就是如何在ubunt…...

K8s ConfigMap的基础功能介绍
在 Kubernetes 中,ConfigMap 是一种用于管理配置信息的资源对象,它允许你将 配置信息与代码解耦,方便管理和更新应用配置,而无需重新构建镜像或重启服务。 ConfigMap 的功能 存储配置信息: 可以以 键值对 的形式存储配…...

Linux——Shell
if 语句 格式:if list; then list; [ elif list; then list; ] ... [ else list; ] fi 单分支 if 条件表达式; then 命令 fi 示例: #!/bin/bash N10 if [ $N -gt 5 ]; then echo yes fi # bash test.sh yes 双分支 if 条件表达式; then 命令 else 命令…...

armsom产品编译烧录Linux固件
1、开发环境及工具准备 Rockchip Linux 软件包:linux-5.10-gen-rkr4 主机: 安装VMware搭建虚拟机,版本为Ubuntu 20.04 (硬盘容量大于100G) 安装远程连接工具MobaXterm(可连接虚拟机方便文件传输) 2、S…...

VSCode:Markdown插件安装使用 -- 最简洁的VSCode中Markdown插件安装使用
VSCode:Markdown插件安装使用 1.安装Marktext2.使用Marktext 本文,将在Visual Studio Code中,安装和使用Markdown插件,以Marktext插件为例。 1.安装Marktext 打开VSCode,侧边栏中找到扩展模块(或CtrlShiftX快捷键)&am…...

AI 行业发展趋势:科技创新引领未来变革
在当今数字化时代,人工智能(AI)行业正以前所未有的速度蓬勃发展,深刻地改变着我们的生活、工作和社会格局。从基础技术的突破到广泛的应用场景拓展,AI 展现出了一系列令人瞩目的发展趋势,预示着一个充满无限可能的未来。 一、技术创新持续突破 模型规模与性能提升 AI 模…...

FB爆款打法实操经验总结
在进行Facebook广告投放时,有效的预算控制、素材测试、广告效果评估和lookalike受众的管理是至关重要的。通过科学的方法和策略,您可以在竞争激烈的市场中实现更好的业绩。 01 预算控制 测试阶段的广告不稳定性:在投放广告的初期,…...

微信小程序TTS解决方案
微信小程序原生语音合成 API(基础且简单) 介绍:微信小程序提供了基础的语音合成能力。通过wx.createInnerAudioContext()等相关API,可以实现简单的语音播放功能。不过它主要是用于音频播放,对于完整的文本到语音&#…...

centos stream 8下载安装遇到的坑
早在2020年12月。CentOS 官方发文宣称:“CentOS项目的未来是 CentOS Stream 明年我们会将重点从CentOS Linux 转移到CentOS Stream 它紧随当前 RHEL 版本之前。CentOS Linux 8 作为 RHEL 8 的重建,将于 2021 年底结束。CentOS Stream 在该日期之后继续&a…...

计算机网络——期末复习(1)背诵
背诵 交换机与路由器:交换机连接同一子网,利用帧中的目的物理地址转发帧,工作在数据链路层;路由器连接不同子网,利用IP数据报中的目的IP地址转发IP数据报,工作在网络层。五层的任务:࿰…...

AORO M6 Pro单北斗防爆终端全面国产化,关键技术不再“卡脖子”
全球科技竞争日益激烈,核心技术自主创新已成为国家发展的战略基石。面对关键技术被“卡脖子”的风险,中国科技企业正加速推进信息技术应用创新战略,力求在关键领域实现自主可控。遨游通讯推出的一款融合单北斗、鸿蒙系统、5G国产芯片的防爆终…...