UITableView 基本使用方法总结
1.、首先,Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource
2、然后 UITableView对象的 delegate要设置为 self。
3、 然后就可以实现这些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; 这个方法返回 tableview 有多少个section
//返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return ;
}
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section; 这个方法返回 对应的section有多少个元素,也就是多少行。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return ;
}
(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 的高度。
(4)- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 返回指定的row 的cell。
这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最前头 叫 cell.imageView 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *showUserInfoCellIdentifier = @"ShowUserInfoCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
if (cell == nil)
{
// Create a cell to display an ingredient.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:showUserInfoCellIdentifier] autorelease];
} // Configure the cell.
cell.textLabel.text=@"签名";
cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
}
(5)- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 返回指定的 section 的 header 的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==)
return 80.0f;
else
return 30.0f;
}
(6)- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == tableView_)
{
if (section == )
{
return @"title 1";
}
else if (section == )
{
return @"title 2";
}
else
{
return nil;
}
}
else
{
return nil;
}
}
(7) - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 返回指定的 section header 的view,如果没有,这个函数可以不返回view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == )
{
UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView" owner: self options: nil] lastObject];
else
{
return nil;
}
}
(8) - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
TableView.allowsSelection=YES;
如果不希望响应select,那么就可以用下面的代码设置属性:
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == )
{
return;
}
else if(indexPath.section==)
{
switch (indexPath.row)
{
//聊天
case :
{
[self onTalkToFriendBtn];
}
break;
default:
break;
}
}
else
{
return ;
}
}
如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置 cell.selectionStyle = UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell被选中后的颜色不变
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
(9)如何设置 tableview 每行之间的 分割线
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone 即可。
(10)如何设置 tableview cell的背景颜色
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//设置背景颜色
cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
}
(11) - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 这个函数响应,用户点击cell 右边的 箭头(如果有的话)
(12)如何设置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];
}
(13)如何获得 某一行的CELL对象 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
UITableView 基本使用方法总结的更多相关文章
- [OC][转]UITableView属性及方法大全
Tip: UITableView属性及方法大全 (摘录地址) p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; line-height: 14.0px; font: 1 ...
- iOS开发UITableView基本使用方法总结1
UITableView基本使用方法 1.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDataSource 2.然后 ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- UITableView优化的方法
使用不透明视图. 不透明的视图可以极大地提高渲染的速度.因此如非必要,可以将table cell及其子视图的opaque属性设为YES(默认值). 其中的特例包括背景色,它的alpha值应该为1(例如 ...
- iOS UITableView的一些方法
项目中用到的一些tabview 问题及对应方法: 一.tableview 1.下划线左对齐 //步骤一:(加在 viewdidload方法中) if([tabView respondsToSelect ...
- UITableView属性和方法
1.初始化一个UITableView - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style struct CGRect { C ...
- IOS 表视图(UITableVIew)的使用方法(7)表视图的编辑功能(拖拉调整排序位置)
除了每个单元行左边的删除和新增图标,UITableView还支持在单元行的右侧显示一个供用户拖拉调整排序位置的控件. 不过如果要显示此控件,UITableView的数据源需要实现以下的方法. -(vo ...
- IOS 表视图(UITableVIew)的使用方法(6)表视图的编辑功能(新增Add)
表视图的新增功能和删除功能虽然目的不同,但是工作流程是相似的 下面列出在处理新增的回调函数时,与删除所不同的逻辑部分代码. 显示下过如下: #pragma mark #pragma mark Tabl ...
随机推荐
- Flutter 控件之 AppBar 和 SliverAppBar
AppBar 和 SliverAppBar 是纸墨设计中的 App Bar,也就是 Android 中的 Toolbar,关于 Toolbar 的设计指南请参考纸墨设计中 Toolbar 的内容. A ...
- Android保持屏幕常亮
Android保持屏幕常亮,PowerManager.WakeLock的使用 package com.hebaijun.wakelock; import android.app.Activi ...
- oracle 中的角色
conn sys as sysdba;create role testrole;grant create session, create table, unlimited tablespace to ...
- JQuery 引用方式
# head里面 <script src="jquery.min.js"></script> <script src="common.js& ...
- 团队开发心得(May)
经过之前一个多月的准备工作(包括去求调研.技术选型.知识储备等等),这个月开发工作终于步入正轨,下面谈谈我的心得体会. 个人收获方面,我选择了加入数据库小组,进行数据库方面的开发.刚开始的时候我是个小 ...
- git工具的使用
Git工具的出现降低了软件版本维护的成本,极大的提高了工作效率,在这里列出了一些使用方法,方便以后查看. 1.Git的初始化->创建一个Git仓库:git init 2.创建信息:git con ...
- PHP匹配当前传入是何种类型
本文出至:新太潮流网络博客 /** * [is_string_regular_type 正则自动验证传入数据] * @E-mial wuliqiang_aa@163.com * @TIME 2017- ...
- SQL Server 索引知识-结构,实现
索引的作用毋庸置疑,但他是如何组织,并实现提高语句访问效率的呢?本篇文章为大家做个详细的介绍. 聚集索引架构 B-tree 如图1-1 a.B-tree的结构,叶子节点为数据.数据按照聚集索引键有序排 ...
- mac os maverick下安装nginx+php-fpm via homebrew
自己虽然平时爱折腾,却很少有记下来的习惯,除非碰到特别多问题的部署才会有冲动.今天看同事折腾git,在旁边看着他mac上的evernote满满的记了好几篇,全是技术相关的记录,忽然很感慨.过去解决了很 ...
- ElasticSearch入坑指南之概述及安装
---恢复内容开始--- ElasticSearch入坑指南之概述及安装 了解ElasticSearch ElasticSearch(简称ES)基于Lucene的分布式全文检索引擎.使用ES可以实现近 ...