UI学习(四)
UITableView基础
dateSource
:数据代理对象
delegate
:普通代理对象
numberOfSectionInTableView
:获得组数协议
numberOfRowsInSection
:获得行数协议
cellForRowAtIndexPath
:创建单元格协议
UIViewController.h:
objectivec
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{
//定义一个数据视图对象
//数据视图用来显示大量相同的格式的大量信息的视图
UITableView* _tableView;
}
@end
ViewController.m:
objectivec
#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:
objectivec
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>
{
//定义数据视图对象
UITableView* _tableview;
//声明一个数据源
NSMutableArray* _arrayData;
}
@end
UIViewController.m:
objectivec
#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
设置一个导航控制器:
objectivec
#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:
objectivec
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
{
//数据视图
UITableView* _tableview;
//数据源
NSMutableArray* _arrayData;
UIBarButtonItem* _btnEdit;
UIBarButtonItem* _btnFinish;
UIBarButtonItem* _btnDelete;
BOOL _isEdit;
}
@end
ViewController.m:
objectivec
#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";
//尝试获取可以复用的单元格
//如果得不到,返回nil
UITableViewCell* 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
效果图 :