【iOS】UI学习——UITableView

UI学习(四)

  • UITableView基础
  • UITableView协议
  • UITableView高级协议和单元格

UITableView基础

dateSource:数据代理对象
delegate:普通代理对象
numberOfSectionInTableView:获得组数协议
numberOfRowsInSection:获得行数协议
cellForRowAtIndexPath:创建单元格协议

UIViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{//定义一个数据视图对象//数据视图用来显示大量相同的格式的大量信息的视图UITableView* _tableView;
}
@end

ViewController.m

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];//创建数据视图//P1:数据视图的位置//P2:数据视图的风格//UITableViewStylePlain:普通风格//UITableViewStyleGrouped:分组风格_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];//设置数据视图的代理对象_tableView.delegate = self;//设置数据视图的数据源对象_tableView.dataSource = self;[self.view addSubview: _tableView];}//获取每组元素的个数(行数)
//程序在显示数据视图时会调用此函数
//返回值:表示每组元素的个数
//P1:数据视图对象本身 P2:那一组需要的行数
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{return 5;
}
//设置数据视图的组数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{return 3;
}//创建单元格对象函数,传入两个参数
//P1:传入这个函数的对象 P2:单元格的索引
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString* cellStr = @"cell";UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];if(cell == nil) {//创建一个单元格对象,传入两个参数//P1:单元格的样式 P2:单元格的副用标记cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];}//indexPath.section表示组数//indexPath.row表示行数NSString* str = [NSString stringWithFormat:@"第%ld组,第%ld行!", indexPath.section, indexPath.row];//将单元格的主文字内容赋值cell.textLabel.text = str;return cell;
}@end

UITableView协议

heightForRowAtIndexPath:获取单元格高度协议
heightForHeaderInSection:数据视图头部高度协议
heightForFooterInSection:数据视图尾部高度协议
titleForFooterINSection:数据视图尾部的标题协议
titleForHeaderInSection:数据视图头部标题协议

UIViewController.h

#import <UIKit/UIKit.h>@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
{//定义数据视图对象UITableView* _tableview;//声明一个数据源NSMutableArray* _arrayData;
}@end

UIViewController.m

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, 480, 832) style:UITableViewStyleGrouped];//设置代理对象_tableview.delegate = self;//设置数据视图代理对象_tableview.dataSource = self;[self.view addSubview:_tableview];//创建一个可变数组_arrayData = [[NSMutableArray alloc] init];for(int i = 'A'; i <= 'Z'; i++) {NSMutableArray* arraySmall = [[NSMutableArray alloc] init];for(int j = 1; j<=5; j++) {NSString* str = [NSString stringWithFormat:@"%c%d", i, j];[arraySmall addObject:str];}//创建一个二维数组[_arrayData addObject: arraySmall];}
}
//获取组数
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{return _arrayData.count;
}
//获取每组的元素个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{NSInteger numRow = [[_arrayData objectAtIndex:section]count];return numRow;
}
//获取单元格
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString *str = @"cell";UITableViewCell *cell = [_tableview dequeueReusableCellWithIdentifier: str];if (cell == nil) {cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: str];}cell.textLabel.text = _arrayData[indexPath.section][indexPath.row];return cell;}
//获取高度
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 100;
}
//获取每组头部标题
-(NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return @"头部标题";}
//获取每组尾部标题
-(NSString*) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{return @"尾部标题";
}-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{return 40;
}-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{return 20;
}@end

效果图
在这里插入图片描述

UITableView高级协议和单元格

高级协议的几个函数
commitEditingStyle:提交编辑函数
canEditRowAtIndexPath:开启关闭编辑单元格
editingStyleForRowAtIndexPath:编辑单元格风格设定
didSelectRowAtIndexPath:选中单元格响应协议
didDeselectRowAtIndexPath:反选单元格响应协议
单元格几个函数
dequeueReusableCellWithIdentifier:获取可以复用的单元格对象
initWithStyle:根据风格创建单元格对象
reuseldentifier:设置可以复用单元格的ID

设置一个导航控制器

#import "SceneDelegate.h"
#import "ViewController.h"
@interface SceneDelegate ()@end@implementation SceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {self.window.frame = [UIScreen mainScreen].bounds;UINavigationController* nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];self.window.rootViewController = nav;
}- (void)sceneDidDisconnect:(UIScene *)scene {// Called as the scene is being released by the system.// This occurs shortly after the scene enters the background, or when its session is discarded.// Release any resources associated with this scene that can be re-created the next time the scene connects.// The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
}- (void)sceneDidBecomeActive:(UIScene *)scene {// Called when the scene has moved from an inactive state to an active state.// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}- (void)sceneWillResignActive:(UIScene *)scene {// Called when the scene will move from an active state to an inactive state.// This may occur due to temporary interruptions (ex. an incoming phone call).
}- (void)sceneWillEnterForeground:(UIScene *)scene {// Called as the scene transitions from the background to the foreground.// Use this method to undo the changes made on entering the background.
}- (void)sceneDidEnterBackground:(UIScene *)scene {// Called as the scene transitions from the foreground to the background.// Use this method to save data, release shared resources, and store enough scene-specific state information// to restore the scene back to its current state.
}@end

ViewController.h:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{//数据视图UITableView* _tableview;//数据源NSMutableArray* _arrayData;UIBarButtonItem* _btnEdit;UIBarButtonItem* _btnFinish;UIBarButtonItem* _btnDelete;BOOL _isEdit;
}@end

ViewController.m:

#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];_tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];//自动调整子视图的大小_tableview.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;//设置代理_tableview.delegate = self;_tableview.dataSource = self;//数据视图头部视图的设定_tableview.tableHeaderView = nil;//数据视图尾部视图的设定_tableview.tableFooterView = nil;[self.view addSubview:_tableview];_arrayData = [[NSMutableArray alloc] init];//初始化数据源数组for(int i = 0; i < 20; i++){NSString* str = [NSString stringWithFormat:@"A %d", i];[_arrayData addObject:str];}//当数据的数据源发生变化时//更新数据视图,重新加载数据[_tableview reloadData];[self createBtn];
}-(void) createBtn
{_isEdit = NO;//设置导航栏按钮_btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编译" style:UIBarButtonItemStyleDone target:self action:@selector(pressEdit)];_btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:nil];_btnFinish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(pressFinish)];self.navigationItem.rightBarButtonItem = _btnEdit;
}-(void) pressEdit
{//修改对象编辑的状态_isEdit = YES;self.navigationItem.rightBarButtonItem = _btnFinish;//开启编辑状态[_tableview setEditing:YES];self.navigationItem.leftBarButtonItem = _btnDelete;
}-(void) pressFinish {_isEdit = NO;self.navigationItem.rightBarButtonItem = _btnEdit;[_tableview setEditing:NO];self.navigationItem.leftBarButtonItem = nil;
}-(NSInteger) tableView:(UITableView*) tableView numberOfRowsInSection:(NSInteger)section
{return _arrayData.count;
}//默认组数返回1
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{return 1;
}-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{NSString* strID = @"ID";//尝试获取可以复用的单元格//如果得不到,返回nilUITableViewCell* cell = [_tableview dequeueReusableCellWithIdentifier:strID];if(cell == nil) {cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];}//单元格文字赋值cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];//设置文字子标题cell.detailTextLabel.text = @"子标题";//为单元格添加图片,设置图标NSString* str = [NSString stringWithFormat:@"%d.png", 12];UIImage* image = [UIImage imageNamed:str];UIImageView* iView = [[UIImageView alloc] initWithImage:image];cell.imageView.image = image;return cell;
}-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{//默认为删除//UITableViewCellEditingStyleInsert 增加//UITableViewCellEditingStyleDone 空return UITableViewCellEditingStyleDelete;
}
//可以显示编辑状态,当手指在单元格上移动时
-(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{//删除数据源对应的数据[_arrayData removeObjectAtIndex:indexPath.item];//数据源更新[_tableview reloadData];NSLog(@"delete");
}-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"选中单元格!%ld %ld", (long)indexPath.section, (long)indexPath.row);
}-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{NSLog(@"取消选中单元格 %ld %ld", (long)indexPath.section, (long)indexPath.row);
}@end

效果图
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/848241.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

JavaScript:从基础到进阶的全面介绍

JavaScript&#xff1a;从基础到进阶的全面介绍 JavaScript&#xff08;简称JS&#xff09;是一种广泛用于Web开发的编程语言。它是一种轻量级的、解释型或即时编译的语言&#xff0c;具有函数优先的特点。JS最初是为了实现网页的动态效果而设计的&#xff0c;如今已发展成为前…

数字取证技术(Digital Forensics Technology)实验课II

数字取证技术(Digital Forensics Technology)实验课II 本文是我本学期的教学课题目,不包含任何博客知识分享,无关的读者可忽略; 实验练习题 (♞思考):请对工作邮件进行签名;“problem3_1.txt"里存储的是由John Doe撰写的真实的邮件,而"problem3_2.txt"里存储的…

ORPC-851(工业级)晶体管光耦,兼容替代LTV-851、PC851

提供隔离反馈 逻辑电路之间的接口 带基极引脚高可靠晶体管输出光耦 电平转换 DC和AC输入 SMPS中的调节反馈电路 消除接地环路 特征 电流传输比 &#xff08; CTR &#xff1a; 最低 50% IF 5mA&#xff0c; VCE 5V &#xff09; 宽工作温度范围 -55~100C 高输入输出隔离…

【python010】获取任意多边形区域内的经纬度点并可视化

1.熟悉、梳理、总结项目研发实战中的Python开发日常使用中的问题、知识点等&#xff0c;如获取任意多边形区域内的经纬度点并可视化&#xff0c;找了N篇文章没发现有效的解决方案。 2.欢迎点赞、关注、批评、指正&#xff0c;互三走起来&#xff0c;小手动起来&#xff01; 3.欢…

【一步一步了解Java系列】:重磅多态

看到这句话的时候证明&#xff1a;此刻你我都在努力 加油陌生人 个人主页&#xff1a;Gu Gu Study专栏&#xff1a;一步一步了解Java 喜欢的一句话&#xff1a; 常常会回顾努力的自己&#xff0c;所以要为自己的努力留下足迹 喜欢的话可以点个赞谢谢了。 作者&#xff1a;小闭…

######## mysql各章节终篇索引 ########

关系图&#xff1a; 事务特性 原子性 持久性 隔离性 隔离级别&#xff08;RC、RR等4个&#xff09; 锁 mvcc 一致性 1、索引 【聚集(聚簇)/非聚集(非聚簇)/二级索引、覆盖索引、主键自增的影响】#### 聚集(聚簇)、非聚集(非聚簇)、二级索引&#xff0c;覆盖索引&#xff0c;及…

低代码开发平台一般都有哪些功能和模块?

在当今快速变化的数字化时代&#xff0c;企业对于高效、灵活且经济的软件开发解决方案的需求愈发迫切。低代码开发平台应运而生&#xff0c;成为众多企业实现数字化转型的首选工具。本文将详细探讨低代码开发平台一般具备的主要功能和模块&#xff0c;以及它们如何助力企业提升…

6月5日 C++day3

#include <iostream>using namespace std;class Per { private:string name;int age;int *high;double *weight; public:Per(){cout << "Per的无参构造" << endl;}Per(string name,int age,int high,double weight):\name(name),age(age),high(new…

trace32 显示用户进程用户态调用栈

在只加载了linux vmlinux符号表的情况下&#xff0c;trace32 只能显示内核态的调用栈函数信息&#xff0c;无法显示用户态调用栈的函数信息&#xff1a; 查看进程maps 确认地址0x40616C为进程/bin/box的虚拟地址&#xff1b;而0xFFFF904E12FC为/lib/libc-2.30.so的地址&#x…

代码随想录算法训练营day44 | 01背包问题 二维、01背包问题 一维、416. 分割等和子集

01背包问题 二维 1. 确定dp数组以及下标的含义 dp[i][j] 表示从下标为[0-i]的物品里任意取&#xff0c;放进容量为j的背包&#xff0c;价值总和最大是多少。 2. 确定递推公式 dp[i][j] max(dp[i - 1][j], dp[i - 1][j - weight[i]] value[i]) 3. dp数组如何初始化 首先从…

有多少苹果用来分赃

题目描述&#xff1a; 有5个人偷了一堆苹果&#xff0c;他们准备在第二天进行分赃。晚上&#xff0c;有一个溜出来&#xff0c;他把所有苹果分成了5份&#xff0c;但是多了一个&#xff0c;他顺手把多的一个苹果扔给树上的猴子&#xff0c;自己先拿1/5藏了起来。没想…

字节跳动Seed-TTS文本到语音模型家族

字节跳动的SEED TTS&#xff08;Seed-TTS&#xff09;是一系列大规模自回归文本转语音&#xff08;TTS&#xff09;模型&#xff0c;能够生成与人类语音几乎没有区别的高质量语音。该模型在语音上下文学习方面表现出色&#xff0c;尤其在说话者相似度和自然度方面的表现&#x…

Python 编程时可能会遇到各种错误提示

下是一些常见的 Python 错误提示及其简要解释&#xff1a; SyntaxError&#xff08;语法错误&#xff09; 示例&#xff1a;File "<stdin>", line 1, in <module> print("Hello, World! &#xff08;缺少闭合括号&#xff09;解释&#xff1a;Pyth…

篇1:Mapbox Style Specification

目录 引言 地图创建与样式加载 Spec Reference Root sources type:vector矢量瓦片

特惠电影票api接口文档,宜选影票接口文档不断优化

宜选影票接口文档的优化是一个持续的过程&#xff0c;旨在提高API的易用性、稳定性和安全性。以下是根据参考文章和相关经验&#xff0c;对宜选影票接口文档优化的一些建议&#xff0c;采用分点表示和归纳的方式&#xff1a; 1. 明确接口目标和功能 清晰定义&#xff1a;在接…

css动画案例练习之会展开的魔方和交错的小块

这里写目录标题 一级目录二级目录三级目录 下面开始案例的练习&#xff0c;建议第一个动手操作好了再进行下一个一、交错的小块效果展示1.大致思路1.基本结构2.实现动态移动 2.最终版代码 二、会展开的魔方1.大致思路1.基本结构;2.静态魔方的构建3.让静态的魔方动起来 2.最终版…

JavaScript第十讲:DOM编程(1):节点概念,如何获取元素节点,节点属性,样式练习题

前言 上一节是本文的知识点讲解&#xff0c;有需要的码客们先看一下&#xff0c;本文是练习题 题目要求 编写一个HTML文档&#xff0c;展示DOM编程的基础知识&#xff0c;包括节点概念的理解、如何获取元素节点、节点属性的操作以及样式调整。要求文档中包含一个带有特定ID的…

【MySQL】表的基本操作

&#x1f30e;表的基本操作 文章目录&#xff1a; 表的基本操作 创建查看表       创建表       查看表结构 表的修改       表的重命名       表的添加与修改       删除表结构 总结 前言&#xff1a; 在数据库中&#xff0c;数据表是存储和组…

vue脚手架 笔记01

01 页面组件 所谓的组件就是把页面每一块内容单独分离出去封装起来 组件包括自己本身的html css 和 js 可以被反复引入使用 (复用) 方便后期维护(方便快速的增加或者删除指定页面的指定模块) 组件化开发: 组件是独立的可复用的代码组织单元 组件系统是vue核心特性之一 组件分类…

鸿蒙状态管理-@Builder自定义构建函数

Builder 将重复使用的UI元素抽象成一个方法 在build方法里调用 使其成为 自定义构建函数 Entry Component struct BuilderCase {build() {Column(){Row(){Text("西游记").fontSize(20)}.justifyContent(FlexAlign.Center).backgroundColor("#f3f4f5").hei…