当前位置: 首页 > 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…...

变量 varablie 声明- Rust 变量 let mut 声明与 C/C++ 变量声明对比分析

一、变量声明设计&#xff1a;let 与 mut 的哲学解析 Rust 采用 let 声明变量并通过 mut 显式标记可变性&#xff0c;这种设计体现了语言的核心哲学。以下是深度解析&#xff1a; 1.1 设计理念剖析 安全优先原则&#xff1a;默认不可变强制开发者明确声明意图 let x 5; …...

Linux应用开发之网络套接字编程(实例篇)

服务端与客户端单连接 服务端代码 #include <sys/socket.h> #include <sys/types.h> #include <netinet/in.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> #include <pthread.h> …...

19c补丁后oracle属主变化,导致不能识别磁盘组

补丁后服务器重启&#xff0c;数据库再次无法启动 ORA01017: invalid username/password; logon denied Oracle 19c 在打上 19.23 或以上补丁版本后&#xff0c;存在与用户组权限相关的问题。具体表现为&#xff0c;Oracle 实例的运行用户&#xff08;oracle&#xff09;和集…...

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

【杂谈】-递归进化:人工智能的自我改进与监管挑战

递归进化&#xff1a;人工智能的自我改进与监管挑战 文章目录 递归进化&#xff1a;人工智能的自我改进与监管挑战1、自我改进型人工智能的崛起2、人工智能如何挑战人类监管&#xff1f;3、确保人工智能受控的策略4、人类在人工智能发展中的角色5、平衡自主性与控制力6、总结与…...

React第五十七节 Router中RouterProvider使用详解及注意事项

前言 在 React Router v6.4 中&#xff0c;RouterProvider 是一个核心组件&#xff0c;用于提供基于数据路由&#xff08;data routers&#xff09;的新型路由方案。 它替代了传统的 <BrowserRouter>&#xff0c;支持更强大的数据加载和操作功能&#xff08;如 loader 和…...

vscode(仍待补充)

写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh&#xff1f; debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...

定时器任务——若依源码分析

分析util包下面的工具类schedule utils&#xff1a; ScheduleUtils 是若依中用于与 Quartz 框架交互的工具类&#xff0c;封装了定时任务的 创建、更新、暂停、删除等核心逻辑。 createScheduleJob createScheduleJob 用于将任务注册到 Quartz&#xff0c;先构建任务的 JobD…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

论文浅尝 | 基于判别指令微调生成式大语言模型的知识图谱补全方法(ISWC2024)

笔记整理&#xff1a;刘治强&#xff0c;浙江大学硕士生&#xff0c;研究方向为知识图谱表示学习&#xff0c;大语言模型 论文链接&#xff1a;http://arxiv.org/abs/2407.16127 发表会议&#xff1a;ISWC 2024 1. 动机 传统的知识图谱补全&#xff08;KGC&#xff09;模型通过…...