当前位置: 首页 > news >正文

iOS开发UITableView基本使用方法总结


本文为大家呈现了iOS开发中UITableView基本使用方法总结。首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource;然后 UITableView对象的 delegate要设置为 self;然后就可以实现这些delegate的一些方法拉。

 

UITableView基本使用方法

1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource

2.然后 UITableView对象的 delegate要设置为 self。

3.然后就可以实现这些delegate的一些方法拉。

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

这个方法返回 tableview 有多少个section

  1. //返回有多少个Sections  
  2. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  3. {  
  4. return 1;  
  5. }  

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

这个方法返回   对应的section有多少个元素,也就是多少行。

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  2. {  
  3. return 10;  
  4. }  

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

这个方法返回指定的 row 的高度。

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

这个方法返回指定的 section的header view 的高度。

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

这个方法返回指定的 section的footer view 的高度。

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";  
  4. UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];  
  5. if (cell == nil)  
  6. {  
  7. // Create a cell to display an ingredient.  
  8. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  
  9. reuseIdentifier:showUserInfoCellIdentifier]  
  10. autorelease];  
  11. }  
  12.  
  13. // Configure the cell.  
  14. cell.textLabel.text=@"签名";  
  15. cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];  
  16. }  

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

返回指定的 section 的header的高度

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (section ==0)  
  4. return 80.0f;  
  5. else  
  6. return 30.0f;  
  7. }  

(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

返回指定的section 的 header  的 title,如果这个section header  有返回view,那么title就不起作用了。

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (tableView == tableView_)  
  4. {  
  5. if (section == 0)  
  6. {  
  7. return @"title 1";  
  8. }  
  9. else if (section == 1)  
  10. {  
  11. return @"title 2";  
  12. }  
  13. else  
  14. {  
  15. return nil;  
  16. }  
  17. }  
  18. else  
  19. {  
  20. return nil;  
  21. }  
  22. }  

(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

返回指定的 section header 的view,如果没有,这个函数可以不返回view

  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  2. {  
  3. if (section == 0)  
  4. {  
  5.  
  6. UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"  
  7. owner: self  
  8. options: nil] lastObject];  
  9.  
  10. else  
  11. {  
  12. return nil;  
  13. }  
  14. }  

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

当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。

  1. TableView.allowsSelection=YES; 
  1. cell.selectionStyle=UITableViewCellSelectionStyleBlue;  

如果不希望响应select,那么就可以用下面的代码设置属性:

  1. TableView.allowsSelection=NO;  

下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. if (indexPath.section == 1)  
  4. {  
  5. return;  
  6. }  
  7. else if(indexPath.section==0)  
  8. {  
  9. switch (indexPath.row)  
  10. {  
  11. //聊天  
  12. case 0:  
  13. {  
  14. [self onTalkToFriendBtn];  
  15. }  
  16. break;  
  17.  
  18. default:  
  19. break;  
  20. }  
  21. }  
  22. else  
  23. {  
  24. return ;  
  25. }  
  26.  
  27. }  

如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置

cell.selectionStyle = UITableViewCellSelectionStyleNone;

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. //cell被选中后的颜色不变  
  4. cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  5. }  

(9)如何设置tableview  每行之间的分割线

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;  

如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone  即可。

(10)如何设置 tableview cell的背景颜色

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. //设置背景颜色  
  4. cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];  
  5. }  

(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath

这个函数响应,用户点击cell 右边的 箭头(如果有的话)

(12)如何设置tableview 可以被编辑

首先要进入编辑模式:

  1. [TableView setEditing:YES animated:YES];  

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

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

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

  1. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3. return UITableViewCellEditingStyleDelete;  
  4. }  

-(void) tableView:(UITableView *)aTableView

commitEditingStyle:(UITableViewCellEditingStyle) editingStyle

forRowAtIndexPath:(NSIndexPath *)indexPath

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

  1. -(void) tableView:(UITableView *)aTableView  
  2. commitEditingStyle:(UITableViewCellEditingStyle) editingStyle  
  3. forRowAtIndexPath:(NSIndexPath *)indexPath  
  4. {  
  5. [chatArray removeObjectAtIndex:indexPath.row];  
  6. [chatTableView reloadData];  
  7. }  

(13)如何获得 某一行的CELL对象

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

转载于:https://www.cnblogs.com/liuqixu/p/4683083.html

相关文章:

  • centos6.5下postgres-XC集群安装与配置(有standby案例)
  • 最近用到Bootstrap Multiselect来详细了解一下
  • python特殊注释
  • MySql | 查询缓存笔记
  • 我的Android进阶之旅------FastJson的简介
  • WebX5 Data判断当前行的值是否改变,以及改变当前行的状态
  • AngularJS学习笔记
  • 前辈的Linux学习心得
  • mysql修改为utf8格式
  • python分析accesslog并排序
  • kali debian linux 的samba 共享设置
  • div如何居中
  • BestCoder Round #50 (div.1) 1001 Distribution money (HDU OJ 5364)
  • Mac 上安装Win10 [missing operating system]解决方法
  • 使用 adb logcat 显示 Android 日志
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【跃迁之路】【463天】刻意练习系列222(2018.05.14)
  • css属性的继承、初识值、计算值、当前值、应用值
  • EventListener原理
  • LeetCode29.两数相除 JavaScript
  • PHP面试之三:MySQL数据库
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • SpringBoot 实战 (三) | 配置文件详解
  • SQL 难点解决:记录的引用
  • vue2.0一起在懵逼的海洋里越陷越深(四)
  • 安装python包到指定虚拟环境
  • 回流、重绘及其优化
  • 机器学习 vs. 深度学习
  • 基于 Babel 的 npm 包最小化设置
  • 利用DataURL技术在网页上显示图片
  • 写给高年级小学生看的《Bash 指南》
  • 学习ES6 变量的解构赋值
  • Nginx实现动静分离
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • 说说我为什么看好Spring Cloud Alibaba
  • #pragma data_seg 共享数据区(转)
  • $分析了六十多年间100万字的政府工作报告,我看到了这样的变迁
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (39)STM32——FLASH闪存
  • (arch)linux 转换文件编码格式
  • (MonoGame从入门到放弃-1) MonoGame环境搭建
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (剑指Offer)面试题41:和为s的连续正数序列
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理第3章 信息系统治理(一)
  • (三)Honghu Cloud云架构一定时调度平台
  • (算法设计与分析)第一章算法概述-习题
  • (一)插入排序
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • (原創) 如何讓IE7按第二次Ctrl + Tab時,回到原來的索引標籤? (Web) (IE) (OS) (Windows)...
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • @Tag和@Operation标签失效问题。SpringDoc 2.2.0(OpenApi 3)和Spring Boot 3.1.1集成
  • [ Linux Audio 篇 ] 音频开发入门基础知识
  • [Android]创建TabBar
  • [C++] cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
  • [C++]AVL树怎么转