UITableView 一些方法

本文详细介绍了UITableView的初始化、数据源配置及委托方法的使用。包括UITableView的初始化步骤、数据源的设置方法,以及UITableViewDelegate中常用方法的解释。同时,还介绍了UITableViewCell的基本属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. UITableView的初始化

[csharp] view plain copy
  1. UITableViewtableview=[[UITableViewalloc]initWithFrame:CGRectMake(0,0,320,420)];
  2. [tableviewsetDelegate:self];
  3. [tableviewsetDataSource:self];
  4. [self.viewaddSubview:tableview];
  5. [tableviewrelease];


(1)在初始化UITableView的时候必须实现UITableView的是,在.h文件中要继承UITableViewDelegate和UITableViewDataSource,并实现3个UITableView数据源方法和设置它的delegate为self,这个是在不直接继承UITableViewController实现的方法。

(2)直接在XCODE生成项目的时候继承UITableViewController的,它会帮你自动写好UITableView必须要实现的方法。

(3)UITableView继承自UIScrollView。

2. UITableView的数据源

(1)UITableView是依赖外部资源为新表格单元填上内容的,我们称为数据源,这个数据源可以根据索引路径提供表格单元格,在UITableView中,索引路径是NSIndexPath的对象,可以选择分段或者分行,即是我们编码中的section和row。

(2)UITableView有三个必须实现的核心方法,分别如下:

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

这个方法可以分段显示或者单个列表显示我们的数据。如下,左边为分段显示,右边为单个列表显示:


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

这个方法返回每个分段的行数,不同分段返回不同的行数可以用switch来做,如果是单个列表就直接返回单个你想要的函数即可。

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

这个方法是返回我们调用的每一个单元格。通过我们索引的路径的section和row来确定。

3. UITableView的委托方法

使用委托是为了响应用户的交互动作,比如下拉更新数据和选择某一行单元格,在UITableView中有很大这种方法供我们选择。

(1) 委托方法讲解

[csharp] view plain copy
  1. //设置Section的数量
  2. -(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{
  3. returnTitleData;
  4. }
  5. //设置每个section显示的Title
  6. -(NSString*)tableView:(UITableView*)tableViewtitleForHeaderInSection:(NSInteger)section{
  7. return@"Andy-清风";
  8. }
  9. //指定有多少个分区(Section),默认为1
  10. -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
  11. return2;
  12. }
  13. //指定每个分区中有多少行,默认为1
  14. -(NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{
  15. }
  16. //设置每行调用的cell
  17. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath{
  18. staticNSString*SimpleTableIdentifier=@"SimpleTableIdentifier";
  19. UITableViewCell*cell=[tableViewdequeueReusableCellWithIdentifier:
  20. SimpleTableIdentifier];
  21. if(cell==nil){
  22. cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault
  23. reuseIdentifier:SimpleTableIdentifier]autorelease];
  24. }
  25. cell.imageView.image=image;//未选cell时的图片
  26. cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
  27. cell.text=@”Andy-清风”;
  28. returncell;
  29. }
  30. //设置让UITableView行缩进
  31. -(NSInteger)tableView:(UITableView*)tableViewindentationLevelForRowAtIndexPath:(NSIndexPath*)indexPath{
  32. NSUIntegerrow=[indexPathrow];
  33. returnrow;
  34. }
  35. //设置cell每行间隔的高度
  36. -(CGFloat)tableView:(UITableView*)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath{
  37. return40;
  38. }
  39. //返回当前所选cell
  40. NSIndexPath*ip=[NSIndexPathindexPathForRow:rowinSection:section];
  41. [TopicsTableselectRowAtIndexPath:ipanimated:YESscrollPosition:UITableViewScrollPositionNone];
  42. //设置UITableView的style
  43. [tableViewsetSeparatorStyle:UITableViewCellSelectionStyleNone];
  44. //设置选中Cell的响应事件
  45. -(void)tableView:(UITableView*)tableViewdidSelectRowAtIndexPath:(NSIndexPath*)indexPath{
  46. [tableViewdeselectRowAtIndexPath:indexPathanimated:YES];//选中后的反显颜色即刻消失
  47. }
  48. //设置选中的行所执行的动作
  49. -(NSIndexPath*)tableView:(UITableView*)tableViewwillSelectRowAtIndexPath:(NSIndexPath*)indexPath
  50. {
  51. NSUIntegerrow=[indexPathrow];
  52. returnindexPath;
  53. }
  54. //设置划动cell是否出现del按钮,可供删除数据里进行处理
  55. -(BOOL)tableView:(UITableView*)tableViewcanEditRowAtIndexPath:(NSIndexPath*)indexPath{
  56. }
  57. //设置删除时编辑状态
  58. -(void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  59. forRowAtIndexPath:(NSIndexPath*)indexPath
  60. {
  61. //右侧添加一个索引表
  62. -(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView{
  63. }


(2) 其他

[csharp] view plain copy
  1. //选中cell时的颜色,在官方文档有如下可以选择
  2. typedefenum{
  3. UITableViewCellSelectionStyleNone,
  4. UITableViewCellSelectionStyleBlue,
  5. UITableViewCellSelectionStyleGray
  6. }UITableViewCellSelectionStyle
  7. //cell右边按钮格式
  8. typedefenum{
  9. UITableViewCellAccessoryNone,//don'tshowanyaccessoryview
  10. UITableViewCellAccessoryDisclosureIndicator,//regularchevron.doesn'ttrack
  11. UITableViewCellAccessoryDetailDisclosureButton,//bluebuttonw/chevron.tracks
  12. UITableViewCellAccessoryCheckmark//checkmark.doesn'ttrack
  13. }UITableViewCellAccessoryType
  14. //是否加换行线
  15. typedefenum{
  16. UITableViewCellSeparatorStyleNone,
  17. UITableViewCellSeparatorStyleSingleLine
  18. }UITableViewCellSeparatorStyle
  19. //改变换行线颜色
  20. tableView.separatorColor=[UIColorblueColor];


4. UITableViewCell

表中的每一行都代表一个UITableViewCell。可以使用图像、文本还有辅助的图标等来自定义你自己的UITableViewCell。你可以自定义你自己的cell如下模型或者像appstore那样的。


UITableViewCell为每个Cell提供了三个可以选择的属性,如下:

  • ltextLabel:填写文本
  • ldetailTextLable:稍微详细的副标题
  • limageView:用来显示你cell的图片,可以通过UIImage来加载。

最后给出一个官方的demo给大家学习下,多实践,不懂的就问下,下节课讲些UITableView应用中实际会出现的问题,比如自定义啊,重用单元格,单元格的数据排序等问题。欢迎大家拍砖。

附上代码:http://download.youkuaiyun.com/detail/qiaoshe/3860668

本文来自http://blog.youkuaiyun.com/qiaoshe/article/details/7026056
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值