UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewDataSource,UITableViewDelegate然后根据需要是实现对应的方法即可。 UITableView有两个默认的内置风格,Plain和Grouped,Plain表明表格视图自身没有真正地在你自己实际地提供任何外观之前提供很多的外观,大部分情况下,它会做的唯一的事情是它会给你这些header和footer。Grouped表格视图是UIKit提供的分组风格。风格的话如果有特别的需求,还可以自定义分组的风格。

页面布局

页面比较简单,一个简单UITableView:

头文件中的不需要声明,需要实现一下协议:

  1. @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
  2.  
  3. @end

Demo实现

声明三个数据用来展示数据:

  1. @interface ViewController ()
  2. {
  3. NSArray *channelArr;
  4. NSMutableArray *filmArr;
  5. NSMutableArray *tvArr;
  6. }
  7. @end

初始化数据:

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3. // Do any additional setup after loading the view, typically from a nib.
  4. channelArr=[[NSArray alloc] initWithObjects:@"电影",@"电视剧",nil];
  5. filmArr=[[NSMutableArray alloc] initWithObjects:@"智取威虎山",@"一步之遥",@"匆匆那年",@"北京爱情故事",nil];
  6. tvArr=[[NSMutableArray alloc] initWithObjects:@"何以笙箫默",@"锋刃",@"陆小凤与花满楼",@"武媚娘传奇",nil];
  7. }

 设置分组的组数:

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  2. NSLog(@"%lu",(unsigned long)channelArr.count);
  3. return [channelArr count];
  4. }

设置分组的标题:

  1. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  2. return [channelArr objectAtIndex:section];
  3. }

设置每个分组下内容的个数:

  1. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  2. NSInteger count=0;
  3. switch (section) {
  4. case 0:
  5. count=[filmArr count];
  6. break;
  7. case 1:
  8. count=[tvArr count];
  9. break;
  10. }
  11. return count;
  12. }

设置每个分组下的具体内容:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  2. UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  3. switch (indexPath.section) {
  4. case 0:
  5. [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];
  6. break;
  7. case 1:
  8. [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];
  9. break;
  10. }
  11. return cell;
  12. }

 设置分组标题和底部的高度:

  1. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  2. return 40;
  3. }
  4. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  5. return 0;
  6. }

 设置点击事件:

  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  2. NSString *content;
  3. switch (indexPath.section) {
  4. case 0:
  5. content=[NSString stringWithFormat:@"%@-%@",channelArr[0],[filmArr objectAtIndex:indexPath.row]];
  6. break;
  7. case 1:
  8. content=[NSString stringWithFormat:@"%@-%@",channelArr[1],[tvArr objectAtIndex:indexPath.row]];
  9. break;
  10. }
  11. UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"当前位置:" message:content delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  12. [alterView show];
  13. }

 最终效果:

源代码:

  1. //
  2. // ViewController.m
  3. // TableView
  4. //http://www.cnblogs.com/xiaofeixiang/
  5. // Created by keso on 15/1/24.
  6. // Copyright (c) 2015年 keso. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12. {
  13. NSArray *channelArr;
  14. NSMutableArray *filmArr;
  15. NSMutableArray *tvArr;
  16. }
  17. @end
  18.  
  19. @implementation ViewController
  20.  
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view, typically from a nib.
  24. channelArr=[[NSArray alloc] initWithObjects:@"电影",@"电视剧",nil];
  25. filmArr=[[NSMutableArray alloc] initWithObjects:@"智取威虎山",@"一步之遥",@"匆匆那年",@"北京爱情故事",nil];
  26. tvArr=[[NSMutableArray alloc] initWithObjects:@"何以笙箫默",@"锋刃",@"陆小凤与花满楼",@"武媚娘传奇",nil];
  27. }
  28. //设置分组的组数
  29. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
  30. NSLog(@"%lu",(unsigned long)channelArr.count);
  31. return [channelArr count];
  32. }
  33. //设置分组的标题
  34. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
  35. return [channelArr objectAtIndex:section];
  36. }
  37. //- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
  38. // return @"我是底部";
  39. //}
  40. //设置每个分组的个数
  41. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  42. NSInteger count=0;
  43. switch (section) {
  44. case 0:
  45. count=[filmArr count];
  46. break;
  47. case 1:
  48. count=[tvArr count];
  49. break;
  50. }
  51. return count;
  52. }
  53. //设置分组中具体的内容
  54. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  55. UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
  56. switch (indexPath.section) {
  57. case 0:
  58. [cell.textLabel setText:[filmArr objectAtIndex:indexPath.row]];
  59. break;
  60. case 1:
  61. [cell.textLabel setText:[tvArr objectAtIndex:indexPath.row]];
  62. break;
  63. }
  64. return cell;
  65. }
  66.  
  67. //分组标题的行高
  68. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
  69. return 40;
  70. }
  71. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
  72. return 0;
  73. }
  74. //选中点击事件
  75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  76. NSString *content;
  77. switch (indexPath.section) {
  78. case 0:
  79. content=[NSString stringWithFormat:@"%@-%@",channelArr[0],[filmArr objectAtIndex:indexPath.row]];
  80. break;
  81. case 1:
  82. content=[NSString stringWithFormat:@"%@-%@",channelArr[1],[tvArr objectAtIndex:indexPath.row]];
  83. break;
  84. }
  85. UIAlertView *alterView=[[UIAlertView alloc] initWithTitle:@"当前位置:" message:content delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
  86. [alterView show];
  87. }
  88. - (void)didReceiveMemoryWarning {
  89. [super didReceiveMemoryWarning];
  90. // Dispose of any resources that can be recreated.
  91. }
  92.  
  93. @end

iOS开发-UITableView常用方法的更多相关文章

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

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  2. iOS开发UITableView基本使用方法总结 分类: ios技术 2015-04-03 17:51 68人阅读 评论(0) 收藏

    本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...

  3. iOS开发,UITableView相关问题

    第一条:UITableViewCell 内容的设置 //文本放到最后 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_dataArr.co ...

  4. iOS开发-UITableView自定义Cell

    UITableView在iOS中开发的重要地位是毋庸置疑的,基本上应用中用到的比例是一半左右,而且大部分情况都是需要自定义单元格的,这样用户看到的App才能更有美感.之前写过UITableView的基 ...

  5. iOS开发 UITableView之cell

    1.cell简介 UITableView的每一行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每一行 U ...

  6. iOS开发-UITableView表格优化

    之前的一篇文章大概讲述了一下UITableView的使用,UITableView在iOS的地位和ListView在Android中的地位基本上算是不相上下,关于ListView的优化网上的也有很多文章 ...

  7. iOS开发UITableView的动画cell

    1.动画cell 针对cell的动画,在Delegate中对cell的layer进行操作: 2.实现代码 #import "ViewController.h" #import &q ...

  8. iOS开发-UITableView滑动视差

    视差滚动是指让多层背景以不同的速度移动,形成立体的运动效果,在Web上应用的比较多,App中倒是见的相对比较少,主要在UITableView中的应用的比较多,尤其是当整个UITableViewCell ...

  9. iOS开发-UITableView顶部图片下拉放大

    关于顶部图片下拉放大,在用户展示的个人中心显示用户个人头像信息,设置UITableView的headerView实现,UITableView继承自UIScrollView,同样的设置UIScrollV ...

随机推荐

  1. HDU - 3577 Fast Arrangement 线段树

    Fast Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. MySQL大事务导致的Insert慢的案例分析

    [问题] 有台MySQL服务器不定时的会出现并发线程的告警,从记录信息来看,有大量insert的慢查询,执行几十秒,等待flushing log,状态query end [初步分析] 从等待资源来看, ...

  3. Javascript中call方法和apply方法用法和区别

    第一次在博客园上面写博客,知识因为看书的时候发现了一些有意思的知识,顺便查了一下资料,就发到博客上来了,希望对大家有点帮助. 连续几天阅读<javascript高级程序设计>这本书了,逐渐 ...

  4. [Luogu5162]WD与积木(多项式求逆)

    不要以为用上Stirling数就一定离正解更近,FFT都是从DP式本身出发的. 设f[i]为i个积木的所有方案的层数总和,g[i]为i个积木的方案数,则答案为$\frac{f[i]}{g[i]}$ 转 ...

  5. BZOJ 3091: 城市旅行 lct 期望 splay

    https://www.lydsy.com/JudgeOnline/problem.php?id=3091 https://blog.csdn.net/popoqqq/article/details/ ...

  6. ASP.NET 构建高性能网站 第6篇

    内存问题概述 和CPU一样,内存也是一个直接影响服务端性能的重要的硬件资源. 一般来说,如果服务端内存不足,从导致以下两个问题产生: 1.       导致服务端把一些原本要写到内存中的数据,写到硬盘 ...

  7. LR监控linux系统资源

    一.检查系统是否安装rpc服务 使用LR监控Linux,首先查看系统是否开启了rpc服务,其次查看Linux系统守护进程rpc.restat是否启动,该进程是必须的.可以通过命令rpcinfo -p来 ...

  8. UIAutomator2.0初始

    1. 先直接上样例,谷歌官方Demo: https://github.com/googlesamples/android-testing 2. 一句话说明改动思路 Most importantly, ...

  9. NBT(NetBIOS Over TCP)名称解析概述

    在微软IP网络中,客户计算机查找其他计算机并与之进行通信的主要手段是利用域名(DNS).但是,使用先前版本的Windows户机也使用NetBIOS协议,将名称解析为IP地址. 通过三种方法解析NetB ...

  10. 【Deep Learning】一、AutoEncoder

    Deep Learning 第一战: 完成:UFLDL教程 稀疏自编码器-Exercise:Sparse Autoencoder Code: 学习到的稀疏参数W1: 参考资料: UFLDL教程 稀疏自 ...