iOS开发UITableView基本使用方法总结1
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 1;
- }
(2)- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section;
这个方法返回 对应的section有多少个元素,也就是多少行。
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- {
- return 10;
- }
(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 的高度。
- - (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 ==0)
- 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 == 0)
- {
- return @"title 1";
- }
- else if (section == 1)
- {
- 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 == 0)
- {
- 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;
- cell.selectionStyle=UITableViewCellSelectionStyleBlue;
如果不希望响应select,那么就可以用下面的代码设置属性:
- TableView.allowsSelection=NO;
下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- if (indexPath.section == 1)
- {
- return;
- }
- else if(indexPath.section==0)
- {
- switch (indexPath.row)
- {
- //聊天
- case 0:
- {
- [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;
iOS开发UITableView基本使用方法总结1的更多相关文章
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- 李洪强iOS开发Swift篇—10_方法
李洪强iOS开发Swift篇—10_方法 一.简单说明 跟其他面向对象语言一样,Swift中的方法可以分为2大类: (1)实例方法(Instance Methods) 在OC中,实例方法以减号(-)开 ...
- IOS开发中重写init方法使用需谨慎
IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图 ...
- iOS开发,UITableView相关问题
第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...
- iOS开发使用半透明模糊效果方法整理
虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包括今年最新发布的iOS8也沿袭了这一设计,甚至在OS X 10.10版Yosemite中也开 ...
- [转]iOS开发使用半透明模糊效果方法整理
转自:http://www.molotang.com/articles/1921.html 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包 ...
- iOS开发-获取属性和方法
iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...
- iOS开发-UITableView自定义Cell
UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...
随机推荐
- Sqli-labs less 42
Less-42 Update更新数据后,经过mysql_real_escape_string()处理后的数据,存入到数据库当中后不会发生变化.在select调用的时候才能发挥作用.所以不用考虑在更新密 ...
- BZOJ2961: 共点圆
好久没发了 CDQ分治,具体做法见XHR的论文… /************************************************************** Problem: 29 ...
- hadoop命令行命令
1. bin/hadoop fs -ls / 查看hdfs根目录下的文件 2. bin/hadoop fs -ls /user 查看user下文件 /就是根目录的意思.
- this的使用、继承、super
1.this的使用 1)可以用于区分局部变量 Person(int age,string name) { this.age=age; this.name=name; } 2)构造方法中,用this调用 ...
- DevSecOps简介(二)
越来越多的组织机构开始采取 DevOps 实践,作为呼应,本文将概括强调很多人认为这一实践缺失的部分:安全.随着 NV (网络虚拟化) 和 NFV (网络功能虚拟化)的使用率逐步攀升,在开发和部署流程 ...
- 翻译 - 元编程动态方法之public_send
李哲 - MAY 20, 2015 原文地址:Metaprogramming Dynamic Methods: Using Public_send 作者:Friends of The Web的开发者V ...
- uva 11324
Problem B: The Largest Clique Given a directed graph G, consider the following transformation. First ...
- 同一网站不同和二级域名和不同子目录的cookie
1.cookie二级域名的实现: 用户其中一个站点登录,而且可以各个子频道间切换,保持登录状态设置Cookie时,使用如下代码即可:setcookie(name,value,expire,path,& ...
- hdu 4061 A Card Game
思路: 分析:假设取的牌顺序是一个序列,那么这种序列在末尾为1时是和取牌序列一一对应的,且是符合“游戏结束时牌恰好被取完”的一种情况. 简证:1.在序列中,任一数 i 的后一个数 j 是必然要放在第 ...
- python urllib2的proxyhandler
1. 再a方法中新建了个opener,使用了代理: proxydict = {} proxydict['http'] = "http://%s:%s"%(ip,port) #pri ...