【iOS】UI学习——UITableView

news/2024/9/23 3:30:59/

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.ppmy.cn/news/1468913.html

相关文章

$MPC 登录MEXC,加速Partisia Blockchain 生态市场进程

Partisia Blockchain 是一个以 MPC 技术方案为基础&#xff0c;具备可审计特性的隐私 Layer1 生态&#xff0c;与此同时&#xff0c;该链通过系列创新的系统架构&#xff0c;能够兼顾高迸发、安全、可拓展性以及可互操作特性。基于系列技术特性&#xff0c;Partisia Blockchain…

【算法系列 | 13】深入解析查找算法之—树表查找

引言 查找算法在计算机科学中扮演着至关重要的角色。它们的效率直接影响到系统的性能和用户体验。树表查找&#xff08;Tree-based Search&#xff09;是一类基于树结构的查找算法&#xff0c;广泛应用于各类数据结构和数据库系统中。 本文将深入介绍树表查找算法的原理、优缺点…

6月11号作业

思维导图 #include <iostream> using namespace std; class Animal { private:string name; public:Animal(){}Animal(string name):name(name){//cout << "Animal&#xff1b;有参" << endl;}virtual void perform(){cout << "讲解员的…

vue2 element组件兼容性问题

1.el-select 聚焦问题 点两次才可以选择选项 <el-select name"XXX" v-model"form.XXX" clearable style"width: 100%":popper-append-to-body"false" popper-class"popper-select-class"change"XXX"><…

..\USER\stm32f10x.h(298): error: #67: expected a “}“

原keil4的示例工程在用keil5打开之后出现报错&#xff1a; ..\USER\stm32f10x.h(298): error: #67: expected a "}" 在去掉手动添加的一个宏定义STM32F10X_HD后即可正常编译&#xff0c;因为KEIL5已经自动添加了

nginx mirror流量镜像详细介绍以及实战示例

nginx mirror流量镜像详细介绍以及实战示例 1.nginx mirror作用2.nginx安装3.修改配置3.1.nginx.conf3.2.conf.d目录下添加default.conf配置文件3.3.nginx配置注意事项3.3.nginx重启 4.测试 1.nginx mirror作用 为了便于排查问题&#xff0c;可能希望线上的请求能够同步到测试…

R语言绘图 --- 桑基图(Biorplot 开发日志 --- 5)

「写在前面」 在科研数据分析中我们会重复地绘制一些图形&#xff0c;如果代码管理不当经常就会忘记之前绘图的代码。于是我计划开发一个 R 包&#xff08;Biorplot&#xff09;&#xff0c;用来管理自己 R 语言绘图的代码。本系列文章用于记录 Biorplot 包开发日志。 相关链接…

JVM产生FullGC的原因有哪些?

JVM产生FullGC的原因有哪些&#xff1f; 在Java虚拟机&#xff08;JVM&#xff09;中&#xff0c;垃圾回收&#xff08;Garbage Collection&#xff0c;简称GC&#xff09;是一个非常重要的机制。GC的目的是自动管理内存&#xff0c;回收不再使用的对象&#xff0c;防止内存泄…