UITableView详解

举报
清雨小竹 发表于 2022/09/25 01:31:56 2022/09/25
【摘要】 -、建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)]; [DataTable setDelegate:self]; [DataTable setDataSource:self]; [self.view ad...

  
  1. -、建立 UITableView
  2. DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];
  3. [DataTable setDelegate:self];
  4. [DataTable setDataSource:self];
  5. [self.view addSubview:DataTable];
  6. [DataTable release];
  7. 二、UITableView各Method说
  8. //Section总数
  9. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  10. return TitleData;
  11. }
  12. // Section Titles
  13. //每个section显示的标题
  14. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  15. return @"";
  16. }
  17. //指定有多少个分区(Section),默认为1
  18. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  19. return 4;
  20. }
  21. //指定每个分区中有多少行,默认为1
  22. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  23. }
  24. //绘制Cell
  25. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  26. static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
  27. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
  28. SimpleTableIdentifier];
  29. if (cell == nil) {
  30. cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  31. reuseIdentifier: SimpleTableIdentifier] autorelease];
  32. }
  33. cell.imageView.image=image;//未选cell时的图片
  34. cell.imageView.highlightedImage=highlightImage;//选中cell后的图片
  35. cell.text=//.....
  36. return cell;
  37. }
  38. //行缩进
  39. -(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath{
  40. NSUInteger row = [indexPath row];
  41. return row;
  42. }
  43. //改变行的高度
  44. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  45. return 40;
  46. }
  47. //定位
  48. [TopicsTable setContentOffset:CGPointMake(0, promiseNum * 44 + Chapter * 20)];
  49. //返回当前所选cell
  50. NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];
  51. [TopicsTable selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];
  52. [tableView setSeparatorStyle:UITableViewCellSelectionStyleNone];
  53. //选中Cell响应事件
  54. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  55. [tableView deselectRowAtIndexPath:indexPath animated:YES];//选中后的反显颜色即刻消失
  56. }
  57. //判断选中的行(阻止选中第一行)
  58. -(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
  59. {
  60. NSUInteger row = [indexPath row];
  61. if (row == 0)
  62. return nil;
  63. return indexPath;
  64. }
  65. //划动cell是否出现del按钮
  66. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  67. }
  68. //编辑状态
  69. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  70. forRowAtIndexPath:(NSIndexPath *)indexPath
  71. {
  72. [topicsTable setContentSize:CGSizeMake(0,controller.promiseNum * 44)];
  73. //右侧添加一个索引表
  74. - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
  75. }
  76. //返回Section标题内容
  77. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  78. }
  79. //自定义划动时del按钮内容
  80. - (NSString *)tableView:(UITableView *)tableView
  81. titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
  82. //跳到指的row or section
  83. [tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  84. 三、在UITableViewCell上建立UILable多行显示
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  86. static NSString *CellIdentifier = @"Cell";
  87. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  88. if (cell == nil) {
  89. cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
  90. UILabel *Datalabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 44)];
  91. [Datalabel setTag:100];
  92. Datalabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  93. [cell.contentView addSubview:Datalabel];
  94. [Datalabel release];
  95. }
  96. UILabel *Datalabel = (UILabel *)[cell.contentView viewWithTag:100];
  97. [Datalabel setFont:[UIFont boldSystemFontOfSize:18]];
  98. Datalabel.text = [data.DataArray objectAtIndex:indexPath.row];
  99. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  100. return cell;
  101. }
  102. //选中cell时的颜色
  103. typedef enum {
  104. UITableViewCellSelectionStyleNone,
  105. UITableViewCellSelectionStyleBlue,
  106. UITableViewCellSelectionStyleGray
  107. } UITableViewCellSelectionStyle
  108. //cell右边按钮格式
  109. typedef enum {
  110. UITableViewCellAccessoryNone, // don't show any accessory view
  111. UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track
  112. UITableViewCellAccessoryDetailDisclosureButton, // blue button w/ chevron. tracks
  113. UITableViewCellAccessoryCheckmark // checkmark. doesn't track
  114. } UITableViewCellAccessoryType
  115. //是否加换行线
  116. typedef enum {
  117. UITableViewCellSeparatorStyleNone,
  118. UITableViewCellSeparatorStyleSingleLine
  119. } UITableViewCellSeparatorStyle
  120. //改变换行线颜色lyttzx.com
  121. tableView.separatorColor = [UIColor blueColor];




  
  1. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. UITableViewCell *cell = [selftableView:tableView cellForRowAtIndexPath:indexPath];
  4. return cell.frame.size.height;
  5. }
  6. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  7. {
  8. NSArray* nibView = [[NSBundle mainBundle] loadNibNamed:@"ManagerTileCell" owner:nil options:nil];
  9. UITableViewCell *cell = [nibView objectAtIndex:0];
  10. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  11. ///
  12. UILabel *label = (UILabel*)[cell viewWithTag:0];
  13. label.text = @"test";
  14. return cell;
  15. }


  


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/8534526

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。