当前位置: 首页 > news >正文

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有点类似的&#xff0c;也有header和footer和body&#xff0c;row。下面给出一个demo // // TableViewTestViewController.m // iosstudy2024 // // Created by figo on 2024/12/9. //#import "TableViewTestViewController.h"interfa…...

金智塔科技喜获CCF中国数字金融大会 GraphRAG竞赛二等奖

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

方案解读:数字化扩展中如何提升多云应用安全能力?

越来越多企业选择上云&#xff0c;拥抱数字化转型。数据显示&#xff0c;在过去一年中&#xff0c;将应用托管至六种不同环境中的企业比例已经翻倍&#xff0c;达到令人震惊的38%。与此同时&#xff0c;应用和流经其的关键数据已成为日益复杂的网络攻击的首选目标&#xff0c;且…...

“年轻科技旗舰”爱玛A7 Plus正式发布,全国售价4999元

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

oracle开窗函数笔记、over()笔记

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

【HarmonyOS】HarmonyOS 和 Flutter混合开发 (一)之鸿蒙Flutter环境安装

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

海外招聘丨卢森堡大学—人工智能和机器学习中的 PI 用于图像分析

雇主简介 卢森堡大学立志成为欧洲最受推崇的大学之一&#xff0c;具有鲜明的国际化、多语言和跨学科特色。 她促进研究和教学的相互影响&#xff0c;与国家息息相关&#xff0c;因其在特定领域的研究和教学而闻名于世&#xff0c;并成为当代欧洲高等教育的创新典范。 她的核…...

LeetCode hot100-85

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

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&#xff0…...

C++ 的衰退复制(decay-copy)

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

vue-cli 5接入模块联邦 module federation

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

【Rust自学】3.6. 控制流:循环

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

【第八节】git与github

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

win如何访问Linux数据库(本地)

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

Windows设置所有软件默认以管理员身份运行

方法一、修改注册表 winr打开运行&#xff0c;输入“regedit”打开注册表&#xff1b; 打开此路径“计算机HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionPoliciesSystem”&#xff1b; 在右侧找到“EnableLUA”&#xff0c;将其值改为0&#xff0c;重启电脑。 …...

前端 计算发布时间(如“1小时前”、“3天前”等)

这样效果&#xff0c;在c端比较常见&#xff0c;通过前端也可以处理 代码如下&#xff1a; // 计算 小时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 通过式功率计 描述 定向/通过式功率传感器在线测量正向和反向功率。在安装、维修和监控发射机、天线和射频发生器时&#xff0c;需要进行这些测量。R&SNRT系列由R&SNRT2功率反射计及各种R&SNRT Zxx定向功率传感器。 由于其测量功…...

QLineEdit限制输入固定字节数(UTF-8编码)

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

基于ubuntu的mysql 8.0安装教程

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

挑战杯推荐项目

“人工智能”创意赛 - 智能艺术创作助手&#xff1a;借助大模型技术&#xff0c;开发能根据用户输入的主题、风格等要求&#xff0c;生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用&#xff0c;帮助艺术家和创意爱好者激发创意、提高创作效率。 ​ - 个性化梦境…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

三体问题详解

从物理学角度&#xff0c;三体问题之所以不稳定&#xff0c;是因为三个天体在万有引力作用下相互作用&#xff0c;形成一个非线性耦合系统。我们可以从牛顿经典力学出发&#xff0c;列出具体的运动方程&#xff0c;并说明为何这个系统本质上是混沌的&#xff0c;无法得到一般解…...

拉力测试cuda pytorch 把 4070显卡拉满

import torch import timedef stress_test_gpu(matrix_size16384, duration300):"""对GPU进行压力测试&#xff0c;通过持续的矩阵乘法来最大化GPU利用率参数:matrix_size: 矩阵维度大小&#xff0c;增大可提高计算复杂度duration: 测试持续时间&#xff08;秒&…...

聊一聊接口测试的意义有哪些?

目录 一、隔离性 & 早期测试 二、保障系统集成质量 三、验证业务逻辑的核心层 四、提升测试效率与覆盖度 五、系统稳定性的守护者 六、驱动团队协作与契约管理 七、性能与扩展性的前置评估 八、持续交付的核心支撑 接口测试的意义可以从四个维度展开&#xff0c;首…...

Swagger和OpenApi的前世今生

Swagger与OpenAPI的关系演进是API标准化进程中的重要篇章&#xff0c;二者共同塑造了现代RESTful API的开发范式。 本期就扒一扒其技术演进的关键节点与核心逻辑&#xff1a; &#x1f504; 一、起源与初创期&#xff1a;Swagger的诞生&#xff08;2010-2014&#xff09; 核心…...

大数据学习(132)-HIve数据分析

​​​​&#x1f34b;&#x1f34b;大数据学习&#x1f34b;&#x1f34b; &#x1f525;系列专栏&#xff1a; &#x1f451;哲学语录: 用力所能及&#xff0c;改变世界。 &#x1f496;如果觉得博主的文章还不错的话&#xff0c;请点赞&#x1f44d;收藏⭐️留言&#x1f4…...

力扣-35.搜索插入位置

题目描述 给定一个排序数组和一个目标值&#xff0c;在数组中找到目标值&#xff0c;并返回其索引。如果目标值不存在于数组中&#xff0c;返回它将会被按顺序插入的位置。 请必须使用时间复杂度为 O(log n) 的算法。 class Solution {public int searchInsert(int[] nums, …...

python报错No module named ‘tensorflow.keras‘

是由于不同版本的tensorflow下的keras所在的路径不同&#xff0c;结合所安装的tensorflow的目录结构修改from语句即可。 原语句&#xff1a; from tensorflow.keras.layers import Conv1D, MaxPooling1D, LSTM, Dense 修改后&#xff1a; from tensorflow.python.keras.lay…...

React---day11

14.4 react-redux第三方库 提供connect、thunk之类的函数 以获取一个banner数据为例子 store&#xff1a; 我们在使用异步的时候理应是要使用中间件的&#xff0c;但是configureStore 已经自动集成了 redux-thunk&#xff0c;注意action里面要返回函数 import { configureS…...