UITableView的基本知识

2024-08-23 00:18
文章标签 uitableview 基本知识

本文主要是介绍UITableView的基本知识,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、UITableView的概念:

UITableView 是iOS中最重要的控件,几乎所有的页面都可以用UITableView完成。

tableView的使用需要遵循代理和数据源,这也是一种非常棒的设计模式,数据源模式可以近似为代理模式。

tableview要引入2个代理UITableViewDelegate,UITableViewDataSource

二、UITableView的基本用法:

1、基本属性:

(1)设置tableview的类型

UITableViewStylePlain            基本类型,分区头标题会悬浮

UITableViewStyleGrouped    分组的类型,分区头标题不会悬浮

//初始化:

UITableView  *tableView =  [[UITableView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)  style:UITableViewStylePlain];

(2)设置背景:

tableView.backgroundColor = [UIColor redColor];

(3)设置分割线:

类型:tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

颜色:tableView.separatorColor = [UIColor blackColor];

位置:tableView. separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);

(4)数据源和代理:

//主要管视图内容

tableView.delegate = self;

//tableView里面的数据管理

tableView.dataSource = self;

[self.view addSubview:tableView];

2、设置 UITableViewDelegate的方法:

//每个section有多少rows:(必须实现的方法)

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;

//返回指定的 row 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

//返回指定的 section的header view 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

//返回指定的 section的footer view 的高度:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

//返回指定的row 的cell:(必须实现的方法)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;

eg:

static NSString* cellname = @"cellname";

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

//从tableveiw的复用池,是不是有可以复用的cell

//dequeueReusableCellWithIdentifier

TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];

//初始化cell

if (cell == nil) {

//xib文件的初始化

cell = [[[NSBundle mainBundle]loadNibNamed:@"TableViewCell" owner:self options:nil]lastObject];

}

//没有点击效果::

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.label.text = self.arr_tv[indexPath.row];

//自定义字体的uifont

cell.label.font = [UIFont fontWithName:self.arr_tv[indexPath.row] size:13];

//    NSLog(@"cellForRowAtIndexPath-%@",cell);

return cell;

}

//  取消选中的效果::

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中

//其他代码

}

//返回指定的 section 的header的高度;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

// 设置section的数量,默认为1

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;

//section由两部分组成,header和footer,分别设置各自需要展示的字符串,也可以自定义显示的样式,需要用到其他的方法

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;

eg:- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

switch (section) {

case 0:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

case 1:

return [titleArray objectAtIndex:section];//提取标题数组的元素用来显示标题

default:

return @"Unknown";

}

}

// 哪些row可以被编辑

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;

// Moving/reordering  移动

// 哪些row可以被移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath;

//如何设置tableview 可以被编辑

[TableView setEditing:YES animated:YES];

如果要退出编辑模式,肯定就是设置为NO

// - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

返回当前cell  要执行的是哪种编辑,下面的代码是 返回 删除  模式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{

return UITableViewCellEditingStyleDelete;

}

//        -(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath{

[chatArray  removeObjectAtIndex:indexPath.row];

[chatTableView  reloadData];

}

//如何获得 某一行的CELL对象

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

3、查看代理的方法:

// 设置对应的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;

// 自定义header和footer

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;

// 设置编辑的样式

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath;



    文/小铭_同学(简书作者)
    原文链接:http://www.jianshu.com/p/9c8ba9d21369
    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

    这篇关于UITableView的基本知识的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



    http://www.chinasem.cn/article/1097813

    相关文章

    关于数据埋点,你需要了解这些基本知识

    产品汪每天都在和数据打交道,你知道数据来自哪里吗? 移动app端内的用户行为数据大多来自埋点,了解一些埋点知识,能和数据分析师、技术侃大山,参与到前期的数据采集,更重要是让最终的埋点数据能为我所用,否则可怜巴巴等上几个月是常有的事。   埋点类型 根据埋点方式,可以区分为: 手动埋点半自动埋点全自动埋点 秉承“任何事物都有两面性”的道理:自动程度高的,能解决通用统计,便于统一化管理,但个性化定

    了解journalctl的基本知识以及命令

    目录 1. 基本知识2. 详细 1. 基本知识 journalctl 是一个用于查询和查看 systemd 日志的命令行工具,它可以访问系统日志、应用程序日志、内核日志等,提供丰富的过滤和查询功能 知识点描述日志文件位置journalctl 日志默认存储在 /var/log/journal/ 目录下。如果该目录不存在,日志存储在内存中日志持久化为了使日志持久化,可以手动创建 /

    Oracle数据库(权限、用户、角色、基本知识介绍)

    Oracle数据库推荐以引用博客: http://blog.csdn.net/leshami/article/details/5611738 http://www.cnblogs.com/jimeper/p/3394635.html http://blog.csdn.net/bob007/article/details/5871126 本编文章内容分布:(1)介绍数据库常见分类(2)

    ffmpeg 视频编码及基本知识

    理论 H264编码原理(简略) 1. 视频为什么需要进行编码压缩 降低视频数据大小,方便存储和传输 2. 为什么压缩的原始数据采用YUV格式 彩色图像的格式是 RGB 的,但RGB 三个颜色是有相关性的。 采用YUV格式,利用人对图像的感觉的生理特性,对于亮度信息比较敏感,而对于色度信息不太敏感,所以视频编码是将Y分量和UV分量分开来编码的,并且可以减少UV分量,比如我们之前说的YUV420

    猫猫学IOS(十二)UI之UITableView学习(上)LOL英雄联盟练习

    猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/44706671 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 源代码 NYViewController的代码 //ps:新建iOS交流学习群:304570962 可以

    双向链表基本知识

    一、基本知识 1、两个指针,可以通过任意一个结点找到其前一个结点和后一个结点 二、基本操作 2.1、创建一个双向链表 Dlink_t *Create_doublelink(){Dlink_t *pdoulink = malloc(sizeof(Dlink_t));if(pdoulink == NULL){return NULL;}pdoulink->phead = NULL;pdou

    IOS 20 发现界面(UITableView)歌单列表(UICollectionView)实现

    发现界面完整效果 本文实现歌单列表效果 文章基于 IOS 19 发现界面(UITableView)快捷按钮实现 继续实现发现界面歌单列表效果 歌单列表Cell实现 实现流程: 1.创建Cell,及在使用UITableView的Controller控制器上注册Cell; 2.获取data列表数据,并调用UITableView的reloadData(),将数据更新到列表; 3.将

    UITableView的性能优化

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 这个代理方法的实现,只在可见的页面是会重复绘制页面的 static NSString *CellIdentifier = @"tableCell"; UITableViewCel

    一文读懂Mysql连接数的基本知识

    目录 前言1. 基本知识2. 常用命令 前言 原先写过一篇SQL Server的知识点,推荐阅读:Sql Server缓冲池、连接池等基本知识(附Demo) 其余知识点推荐阅读: java框架 零基础从入门到精通的学习路线 附开源项目面经等(超全)Mysql优化高级篇(全)Mysql底层原理详细剖析+常见面试题(全) 1. 基本知识 连接总数(Total Connecti

    Windows—线程基本知识和线程同步

    线程 线程的组成 线程的内核对象,操作系统用它来对线程实施管理。内核对象也是系统用来存放线程统计信息的地方。线程堆栈,它用于维护线程在执行代码时需要的所有函数参数和局部变量 线程的进入点 每个线程必须拥有一个进入点函数,线程从这个进入点开始运行。 主线程的进入点函数:即main函数。如果想要在你的进程中创建一个辅助线程,它必定也是个进入点函数,类似下面: DWORD WINAPI Th