UI学习——UITableView

这篇具有很好参考价值的文章主要介绍了UI学习——UITableView。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


一、UITableView基础

1.基本概念

UITableView是iOS开发中的一个控件,用于展示列表数据。它类似于HTML中的表格(table),但更加强大和灵活。UITableView可以支持滚动、选中、插入、删除等各种操作,并且可以高度自定义。

2.基本用法

(1)创建UITableView实例;
(2)设置UITableView的delegate和dataSource;
(3)实现UITableViewDelegate和UITableViewDataSource协议中的方法;
(4)将UITableView添加到视图中。

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<
//实现数据视图的普通协议
//数据视图的普通事件处理
UITableViewDelegate,
//实现数据视图的数据代理协议
//处理数据视图的数据代理
UITableViewDataSource
>
{
    //定义一个数据视图对象
    //数据视图用来显示大量相同格式的信息的视图
    //例如:电话通讯录、QQ好友、微信朋友圈
    UITableView* _tableview;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建数据视图
    //P1:数据视图的位置
    //P2:数据视图的风格
    //UITableViewStylePlain普通风格
    //UITableViewStyleGrouped分组风格
    _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //设置数据视图的代理对象
    _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 5;
}
//创建单元格对象函数
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* cellStr = @"cell";
    UITableViewCell* cell = [_tableview dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        //创建一个单元格对象
        //参数一:单元格样式
        //参数二:单元格的复用标记
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    //section表示组,row表示行
    NSString* str = [NSString stringWithFormat:@"第%ld组, 第%ld行", indexPath.section, indexPath.row];
    //将单元格的主文字内容赋值
    cell.textLabel.text = str;
    return  cell;
}

@end

uitableview,ui,学习,ios

二、UITableView协议

前面已经介绍了UITableView协议中三个必须实现的函数,下面介绍其协议中剩下可选择实现的函数。

//获取高度
- (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 60;
}

下面是具体的代码实现:
ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
{
    //定义数据视图对象
    UITableView* _tableView;
    NSMutableArray* _arrayData ;
}

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //创建数据视图对象
     _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) 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 = 0; 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* cellStr = @"cell";
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:cellStr];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
    }
    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 60;
}
@end

uitableview,ui,学习,ios

三、UITableView高级协议和单元格

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

_tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    //自动调整子视图的大小
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    //设置代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    //头部视图和尾部视图可以用来展示与整个列表相关的信息,例如搜索框、筛选器、广告等。它们的高度可以根据内容自动调整,也可以手动设置高度。可以通过UITableView的dataSource方法tableView(_:viewForHeaderInSection:)和tableView(_:viewForFooterInSection:)来设置头部视图和尾部视图,或者通过UITableView的属性tableHeaderView和tableFooterView来设置静态的头部视图和尾部视图。
    //数据视图的头部视图的设定
    _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];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _arrayData.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString* str = @"cell";
    //尝试获取可以复用的单元格
    //如果得不到则返回nil
    //复用单元格的条件是单元格的数量足够多使得能够充满屏幕
    UITableViewCell* cell = [_tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    //单元格文字赋值
    cell.textLabel.text = [_arrayData objectAtIndex:indexPath.row];
    //单元格添加子标题
    cell.detailTextLabel.text = @"子标题";
    NSString* istr = @"04.jpg";
    UIImage* image = [UIImage imageNamed:istr];
    //设置默认的图标信息
    cell.imageView.image = image;
    return  cell;
}
//设置单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
//创建按钮
- (void)createBtn {
    _isEdit = NO;
    _btnEdit = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleDone target:self action:@selector(pressEdit)];
    _btnFinish = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(pressFinish)];
    _btnDelete = [[UIBarButtonItem alloc] initWithTitle:@"删除" style:UIBarButtonItemStyleDone target:self action:@selector(pressDelete)];
    self.navigationItem.rightBarButtonItem = _btnEdit;
}
//当手指在单元格上移动时可以显示编辑状态
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    //删除数据源对应的数据
    [_arrayData removeObjectAtIndex:indexPath.row];
    //数据源更新
    [_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);
}
//单元格显示效果协议
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    //UITableViewCellEditingStyleDelete:删除,默认情况下为此状态
    //UITableViewCellEditingStyleInsert:插入
    //UITableViewCellEditingStyleNone:虚空
    //UITableViewCellEditingStyleDelete |UITableViewCellEditingStyleInsert:多选状态
    return UITableViewCellEditingStyleDelete;
}
- (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;
}
- (void)pressDelete {
    
}

uitableview,ui,学习,ios
点击编辑按钮时

uitableview,ui,学习,ios

移动单元格时
uitableview,ui,学习,ios
点击delete后

uitableview,ui,学习,ios

总结

以上就是本篇文章的全部内容。文章来源地址https://www.toymoban.com/news/detail-785906.html

到了这里,关于UI学习——UITableView的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • iOS从UI内存地址到读取成员变量(oc/swift)

    开发调试时,我们发现bug时常首先是从UI显示发现异常,下一步才会去定位UI相关连的数据的。XCode有给我们提供一系列debug工具,但是很多人可能还没有形成一套稳定的调试流程,因此本文尝试解决这个问题,顺便提出一个 暴论 :UI显示异常问题只需要两个步骤就能完成定位

    2023年04月19日
    浏览(62)
  • UI界面视觉设计之字体要素--安卓-ios-网页常用字体

     怎么设计出从而设计出富有美感和形式感的优秀作品? 1.设计经验的积累。 2. 在每个项目设计中只使用1到2个字体样式,通过对字体大小或颜色来强调重点文案,如图的界面设计中,都是通过字体大小、粗细来区分界面内容中的层级关系。字体用得越多,越显得不够专业;

    2024年02月05日
    浏览(58)
  • iOS开发Swift-3-UI与按钮Button-摇骰子App

    1.创建新项目Dice  2.图标  删去AppIcon,将解压后的AppIcon.appiconset文件拖入Assets包。  3.将素材点数1-6通过网页制作成2x,3x版本并拖入Asset。  4.设置对应的UI。  5.拖入Button组件并设置style。  6.Ctrl加拖拽将Button拖拽到ViewController里,并设置Connection,Name等,并点击Connect。  同样

    2024年02月11日
    浏览(32)
  • 【漏洞修复】Cisco IOS XE软件Web UI权限提升漏洞及修复方法

    关于Cisco IOS XE软件Web UI权限提升漏洞及修复方法 Cisco IOS XE Unauthenticatd Remote Command Execution (CVE-2023-20198) (Direct Check) Severity:Critical Vulnerability Priority Rating (VPR): 10.0 Risk Factor: Critical CVSS v3.0 Base Score 10.0 运行Cisco IOS XE软件版本 16.x及更高版本 的产品才会受到影响。 Nexus产品、ACI、传

    2024年02月03日
    浏览(32)
  • docker简单web管理docker.io/uifd/ui-for-docker

    要先pull这个镜像docker.io/uifd/ui-for-docker 这个软件默认只能使用9000端口,别的不行,因为作者在镜像制作时已加入这一层 刚下下来镜像可以通过docker history docker.io/uifd/ui-for-docker 查看到这个端口已被 设置 如果在没有设置br0网关时,可以 用这种默认让bridge模式nat方式访问web: (

    2024年02月14日
    浏览(31)
  • iOS开发Swift-12-列表UI,TableViewController,动态响应Button勾选-待办事项App(1)

    1.创建新项目 为项目添加图标 2.将Table View Controller添加到界面中 将箭头移动到Table View上来,代表它是首页(根页面).选中ViewController,点击Delete,对它进行删除.将代码ViewController.swift也删除掉. 新建一个Cocoa Touch Class. 将TableViewController的class设置成TodosViewController. 2.为cell取名为TodoC

    2024年02月09日
    浏览(39)
  • element-ui的el-select在ios移动端的兼容性适配

    今天的我在做一个 h5 的项目,发现 element-plus (我使用的版本是 2.2.19 )里的 el-select 在 ios 上需要点击2次才能打开下拉框。经过很久的研究和查阅无数资料(包括百度、谷歌搜索到的),都没有解决这个问题。最后我探究里面的根本原因,最后才自己找到解决方案, 可以仔

    2024年01月18日
    浏览(31)
  • Reactive UI -- 反应式编程UI框架入门学习(一)

    反应式编程是一种相对于命令式的编程范式,由函数式的组合声明来构建异步数据流。要理解这个概念,可以简单的借助Excel中的单元格函数。   上图中,A1=B1+C1,无论B1和C1中的数据怎么变化,A1中的值都会自动变化,这其中就蕴含了反应式/响应式编程的思想。 反应式编程对

    2024年02月02日
    浏览(37)
  • 【Flutter·学习实践·UI篇】基础且重要的UI知识

    参考学习官网:《Flutter实战·第二版》  学习前先记住:Flutter 中万物皆为Widget,心中默念3次以上铭记于心。 这一点和开发语言Dart的变量一切皆是对象的概念,相互对应。 在前面的介绍中,我们知道在Flutter中几乎所有的对象都是一个 widget 。与原生开发中“控件”不同的是

    2024年02月15日
    浏览(25)
  • 什么是UI设计?UI学习一般分几个阶段呢?

    什么是UI设计 首先我们先来看下什么是UI,UI即UserInterface(用户界面)的简称。UI设计是指对软件的人机交互、操作逻辑、界面美观的整体设计。目前UI设计师主要从事移动终端的界面化设计和交互式应用。兄弟连作为早期创立的IT培训企业,在UI培训领域一直保持专业的教育模式

    2024年02月06日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包