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

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. // Configure the cell.
  13. [email protected]"签名";
  14. cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
  15. }

(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. UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
  6. owner: self
  7. options: nil] lastObject];
  8. else
  9. {
  10. return nil;
  11. }
  12. }

(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. default:
  18. break;
  19. }
  20. }
  21. else
  22. {
  23. return ;
  24. }
  25. }

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

cell.selectionStyle = UITableViewCellSelectionStyleNone;

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

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

  1. self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

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

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

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. //设置背景颜色
  3. 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 可以被编辑

首先要进入编辑模式:

  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. [chatArray removeObjectAtIndex:indexPath.row];
  5. [chatTableView reloadData];

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

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

时间: 2024-08-26 08:36:39

iOS开发UITableView基本使用方法总结1的相关文章

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

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

iOS开发 UITableView的方法和属性总结

本文描述UITableView的各种方法,属性,委托以及数据源.本文的目的只是总结UITableView的用法,详细的例子另撰文描述. 1 数据源  UITableViewDataSource协议 01 返回组(节)的个数,默认是返回1,如果只有1组数据,可以不用实现该方法. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 02 返回某一组的行数,该组由section的值决定 - (NSInteger)table

IOS开发之——四种方法解析Jason数据(转)

本文将介绍TouchJson. SBJson .JSONKit 和 iOS5所支持的原生的json方法,解析国家气象局API,TouchJson和SBJson需要下载他们的库 TouchJson包下载: http://download.csdn.net/detail/enuola/4523169 SBJson 包下载: http://download.csdn.net/detail/enuola/4523177 JSONKit包下载:http://download.csdn.net/detail

IOS开发中重写init方法使用需谨慎

IOS开发中重写init方法使用需谨慎 今天在写一个小软件的时候出现一点问题,这个软件的功能是搜索全国学校,首页就是搜索输入框,在框中输入完要查询的学校所在省份,点击buttom后就会跳转到对应的视图控制器中,然后把搜索结果呈现在一个TableView上,但是我在调试时,每次输入完然后点击搜索按钮时,弹出结果列表总是空的,我需要返回到首页再点击一次搜索才会出现结果,而且我在首页更改搜索关键字之后,点击搜索,结果还是上次的搜索结果,必须返回点击一次才会出现这次的搜索结果. 经过大神指点,原来这个问

IOS开发UITableView性能应用技巧TableViewCell的重用

?iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存.要解决该问题,需要重用UITableViewCell对象??重用原理:当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用.当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象

iOS开发,UITableView相关问题

第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.count - 1 inSection:0]; [_tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES]; //刷新指定cell NSIndexP

iOS开发使用半透明模糊效果方法整理

虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包括今年最新发布的iOS8也沿袭了这一设计,甚至在OS X 10.10版Yosemite中也开始大量使用半透明模糊. 在iOS开发当中,我们有很多选择可以做半透明模糊效果,下面就是一些常见的方式或者说工具. 0. Core Image 作为设计和体验方面的领导者,苹果自己对图片效果和图片处理的支持一定是非常好的,在iOS平台上,5.0之后就出现了Core Image的API.Core Ima

【转载】iOS开发使用半透明模糊效果方法整理

[转载自 http://www.cocoachina.com/ios/20141223/10731.html] 虽然iOS很早就支持使用模糊效果对图片等进行处理,但尤其在iOS7以后,半透明模糊效果得到大范围广泛使用.包括今年最新发布的iOS8也沿袭了这一设计,甚至在OS X 10.10版Yosemite中也开始大量使用半透明模糊. 在iOS开发当中,我们有很多选择可以做半透明模糊效果,下面就是一些常见的方式或者说工具. 0. Core Image 作为设计和体验方面的领导者,苹果自己对图片效果

iOS开发UItableview的常用属性方法的使用

有些属性和方法始终是记不清,只能记下来,方便查找 如果对你有帮助请支持,没有帮助请告诉我哪里需要改进!谢谢! //  ViewController.m //  TableViewAll #import "ViewController.h" @interface ViewController ()<UITableViewDelegate, UITableViewDataSource> @end @implementation ViewController - (void)vi