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

http://www.lryc.cn/news/505711.html

相关文章:

  • 金智塔科技喜获CCF中国数字金融大会 GraphRAG竞赛二等奖
  • 方案解读:数字化扩展中如何提升多云应用安全能力?
  • “年轻科技旗舰”爱玛A7 Plus正式发布,全国售价4999元
  • oracle开窗函数笔记、over()笔记
  • 【HarmonyOS】HarmonyOS 和 Flutter混合开发 (一)之鸿蒙Flutter环境安装
  • 海外招聘丨卢森堡大学—人工智能和机器学习中的 PI 用于图像分析
  • LeetCode hot100-85
  • linux 内核数据包处理中的一些坑和建议
  • C++ 的衰退复制(decay-copy)
  • vue-cli 5接入模块联邦 module federation
  • 【Rust自学】3.6. 控制流:循环
  • 【第八节】git与github
  • win如何访问Linux数据库(本地)
  • Windows设置所有软件默认以管理员身份运行
  • 前端 计算发布时间(如“1小时前”、“3天前”等)
  • shardingjdbc 4.0.0 seata分布式事务Failed to fetch schema问题
  • 罗德与施瓦茨NRT2功率反射仪,NRT2通过式功率计
  • QLineEdit限制输入固定字节数(UTF-8编码)
  • 基于ubuntu的mysql 8.0安装教程
  • K8s ConfigMap的基础功能介绍
  • Linux——Shell
  • armsom产品编译烧录Linux固件
  • VSCode:Markdown插件安装使用 -- 最简洁的VSCode中Markdown插件安装使用
  • AI 行业发展趋势:科技创新引领未来变革
  • FB爆款打法实操经验总结
  • 微信小程序TTS解决方案
  • centos stream 8下载安装遇到的坑
  • 计算机网络——期末复习(1)背诵
  • AORO M6 Pro单北斗防爆终端全面国产化,关键技术不再“卡脖子”
  • ubuntu 卸载 MySQL