欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 【iOS】UI学习——UITableView

【iOS】UI学习——UITableView

2024/10/25 0:30:04 来源:https://blog.csdn.net/2301_79847748/article/details/139424756  浏览:    关键词:【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

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com