一,UITableView控件使用必备,红色部分是易错点和难点

首先创建一个项目,要使用UITableView就需要实现协议<UITableViewDataSource>,数据源协议主要完成对tableView外观设置,必须实现的方法有两个:

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

着重理解:UITableViewDataSource是数据源协议,主要控制tableView设置的数据,比如行高,页眉页脚,分组等

1,首先在ViewController里面实例化UITableView

- (void)viewDidLoad
{
    [super viewDidLoad];
    UITableView *tableView=[[UITableView alloc]initWithFrame:self.view.bounds];

tableView.dataSource=(id)self;
    [self.view addSubview:tableView];
    // Do any additional setup after loading the view, typically from a nib.
}
2,实现必须的两个方法

这个方法主要实现在每个section里面有几行

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 14;
}

  这个是在每行里面创建cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.backgroundColor=[UIColor grayColor];
    return cell;
}

3,其他常用方法

设置在tableView中有几个section

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

这样就完成了一个最简单的tableview

//------------------------------------------------------------------------------------------------------

4,进阶练习

下面的例子中aa,bb数组可以是省份数组,这样就可以对省份进行分组简单应用,本例子简单,一看就应该明白

- (void)viewDidLoad
{
    [super viewDidLoad];

//创建两个数组,实际应用中可以是任何数据源,此处为了练习分组
    _aa=[NSArray arrayWithObjects:@"11",@"22", nil];
    _bb=[NSArray arrayWithObjects:@"QQ",@"DD",@"EE",@"FF", nil];

//首先必须实例化一个tableView对象,然后加入到根视图
    _bb=[NSArray arrayWithObjects:@"QQ",@"DD",@"EE",@"FF", nil];
    UITableView *tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];这里的style一共有两种格式,另一个是UITableViewStylePlain平铺模式,分组模式一般都是在多个section的时候才使用,运行此程序看看两者的区别
    tableView.dataSource=(id)self;//此处是最容易忘记添加的地方,如果没有这个部分的声明,下面的代理方法都没用.
    [self.view addSubview:tableView];
    // Do any additional setup after loading the view, typically from a nib.
}
//ios系统赋予的内存警告,如果内存不够用的时候会触发此方法
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//设置tableView中section的个数,也就是分组个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

//每组的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //如果是0 section,则在第一部分显示aa数组里的内容
    if (section==0) {//如果是第一组,则返回aa数组中的个数
        return self.aa.count;
    }//如果是1 section,则在第二部分显示bb数组里的内容更
    else if(section==1)
        return self.bb.count;
    return 14;
}
//创建cell,当滚动的时候也会触发此方法,可以动态生成cell,节约系统资源,后面会详解
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    if (indexPath.section==0) {
        cell.textLabel.text=self.aa[indexPath.row];
    }
    else if(indexPath.section==1)cell.textLabel.text=self.bb[indexPath.row];
    
    cell.backgroundColor=[UIColor grayColor];
    return cell;
}
@end
运行以上程序查看效果如图:

查看手机会发现很多应用的分组都有标题,如下(阅读列表),还有页脚(使用....)这些都是可以设置的,以上程序运行出来之后,我们来扩展页眉页脚

//此方法返回字符串,这个字符串就是放在页眉部分的字
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return section==0?@"aa数组内容":@"bb数组内容";
}
//此方法返回字符串,这个字符串就是放在页脚部分的字
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return section==0?@"aa数组页脚":@"bb数组页脚";
}

UITableView详解(1)的更多相关文章

  1. UITableView 详解 ()

    (原本取至D了个L微信公众号) UITableView 详解 一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRec ...

  2. UITableView详解(2)

    承接上文,再续本文 一,首先我们对上次的代码进行改进,需要知道的一点是,滚动视图的时候,我们要创建几个视图,如果一个视图显示一个图片占据整个屏幕,对于滚动视图我们只需要创建三个视图就可以显示几千给图片 ...

  3. 【转】UITableView详解(UITableViewCell

    原文网址:http://www.kancloud.cn/digest/ios-1/107420 上一节中,我们定义的cell比较单一,只是单调的输入文本和插入图片,但是在实际开发中,有的cell上面有 ...

  4. UITableView详解(转)

    首先.对UITableView进行讲解,下面有对它进行实际的应用 UITableView 显示大型内容的列表 单行,多列 垂直滚动,没有水平滚动 大量的数据集 性能强大,而且普遍存在于iPhone的应 ...

  5. UITableView 详解 教程

    看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪称UIKit ...

  6. UITableView详解

    一.建立 UITableView DataTable = [[UITableView alloc] initWithFrame:CGRectMake(, , , )]; [DataTable setD ...

  7. 《iOS 7 应用开发实战详解》

    <iOS 7 应用开发实战详解> 基本信息 作者: 朱元波    管蕾 出版社:人民邮电出版社 ISBN:9787115343697 上架时间:2014-4-25 出版日期:2014 年5 ...

  8. iPhone应用开发 UITableView学习点滴详解

    iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...

  9. IOS中表视图(UITableView)使用详解

    IOS中UITableView使用总结 一.初始化方法 - (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)styl ...

随机推荐

  1. Android存储数据方式

    可以查看Android开发文档中的:/docs/guide/topics/data/data-storage.html Android provides several options for you ...

  2. SO修改

    FUNCTION Z_SD_SALESORDER_CHANGE1. *"----------------------------------------------------------- ...

  3. if条件语句

    第四天 XMind 思维导图复习之前知识 数据类型-变量常量-运算符(表达式)-语句(顺序.分支.循环)-数组-函数 1.if语句格式 if(表达式) { 语句 } 注意: 1.如果,表达式成立,只执 ...

  4. Front End中Javascript兼容问题收集(转)

    1 select标签,就有诸多不兼容: A. cloneNode方法,对于非IE浏览器没有问题,对于IE浏览器, 遇到的问题包括:     1)option selected的会clone不过去,然后 ...

  5. mvc学习记录

    1.关于mvc中的session在controller中传递 在用mvc开发新项目的时候,不久就遇到一个头大的问题,session在controller中传递居然出现空值,但明明一开始就赋值了,通过度 ...

  6. Java 生成压缩包,ZipOutputStream的使用

    案例:根据url 获取网络资源A,B,C   将资源A,B,C放在一起生成一个xxx.zip 直接看代码 import java.io.File; import java.io.FileOutputS ...

  7. Linux查看程序端口占用情况【转】

    今天发现服务器上Tomcat 8080端口起不来,老提示端口已经被占用. 使用命令: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat ...

  8. FZU 2090 旅行社的烦恼 floyd 求无向图最小环

    题目链接:旅行社的烦恼 题意是求无向图的最小环,如果有的话,输出个数,并且输出权值. 刚刚补了一发floyd 动态规划原理,用了滑动数组的思想.所以,这个题就是floyd思想的变形.在k从1到n的过程 ...

  9. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  10. 神奇的NOIP模拟赛 T1 LGTB 玩扫雷

    LGTB 玩扫雷 在一个n m 的棋盘上,有位置上有雷(用“*” 表示),其他位置是空地(用“.” 表示).LGTB 想在每个空地上写下它周围8 个方向相邻的格子中有几个雷.请帮助他输出写了之后的棋盘 ...