一:基础部分

UITableView的两种样式:

注意是只读的

1.UITableViewStytlePlain(不分组的)

n

2.UITableViewStyleGrouped(分组的)

二:如何展示数据

1.

(1)UITableView需要一个数据源(dataSource)来显示数据;

需要注意dataSource是UITableView的一个属性,类型是id(任意形),但是需要遵守<UITableViewDataSource>协议!

(2)UITableView会向数据源查询一共多少行数据以及每一行显示什么数据等;

(3)没有设置数据源的UITableView只是一个空壳;

(4)凡是遵守UITableViewDataSource协议的OC对象,都可以是UITableView的数据源;

UITableViewDataSource协议代码:

有这样两个必须实现的方法

@required

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

//每一行有多少组(section)数据

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:

// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

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

//每一行显示什么内容

还有一个常用的optional的方法

@optional

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented

//这个表示共计多少组数据

三:代码设置UITableView

//

//  ViewController.m

//  tableView

//

//  Created by Mac on 15/12/26.

//  Copyright © 2015年 Mac. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

//    1.设置tableView的数据源,也就是ViewController自己,它自己也是个OC对象且遵守UITableViewDataSource协议!

self.tableView.dataSource =  self;

}

- (BOOL)prefersStatusBarHidden{

return YES;

}

#pragma mark - tableView的数据源方法

//1.设置每一组有多少行数据

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

if (section == 0) {

return 2;

}else{

return 3;

}

}

//2.设置总计多少组

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 2;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

//3.设置每一行的内容

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

//其中,indexPath表示索引

//如:indexPath.row表示某一行,index.section表示某一组

{

UITableViewCell *cell = [[UITableViewCell alloc]init];

//    将第一组第一行的cell的内容设置为红色的“征里”;

if (indexPath.section == 0) {

if (indexPath.row == 0) {

cell.textLabel.text = @"征里";

cell.textLabel.textColor = [UIColor redColor];

return cell;

}else{

cell.textLabel.text = @"征里";

return cell;

}

}else {

cell.textLabel.text = @"征里";

return cell;

}

}

//4.设置每组脚与头显示

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

if (section == 0) {

return @"第一组的征里!";

}else{

return @"第二组的征里!";

}

}

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

{

if (section == 0) {

return @"第一组的征里 @@@";

}else{

return @"第二组的征里 @@@";

}

}

@end

效果如下

12-26 tableView的学习心得的更多相关文章

  1. python学习心得第四章

     python 学习心得第四章 1.lambda表达式 1:什么是lambda表达式 为了简化简单函数的代码,选择使用lambda表达式 上面两个函数的表达式虽然不一样,但是本质是一样的,并且lamb ...

  2. windows类书的学习心得(转载)

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  3. 别人的的MYSQL学习心得(十五) 日志

    我的MYSQL学习心得(十五) 日志 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  4. 我的MYSQL学习心得 mysql日志

    这一篇<我的MYSQL学习心得(十五)>将会讲解MYSQL的日志 MYSQL里的日志主要分为4类,使用这些日志文件,可以查看MYSQL内部发生的事情. 分别是 1.错误日志:记录mysql ...

  5. 我的MYSQL学习心得(八)

    原文:我的MYSQL学习心得(八) 我的MYSQL学习心得(八) 我的MYSQL学习心得(一) 我的MYSQL学习心得(二) 我的MYSQL学习心得(三) 我的MYSQL学习心得(四) 我的MYSQL ...

  6. linux学习心得之目录树开端与/etc(图文)

    linux学习心得之目录树开端与/etc(图文) linux中“一切皆文件”,学习linux一年了,在学习过程中对目录树的一点心得,分享给大家,有不对的地方敬请斧正. 不多说了,先上图: 根目录: / ...

  7. windows类书的学习心得

    原文网址:http://www.blogjava.net/sound/archive/2008/08/21/40499.html 现在的计算机图书发展的可真快,很久没去书店,昨日去了一下,真是感叹万千 ...

  8. 20145335郝昊 Java学习心得 密码学代码复写

    20145335郝昊 Java学习心得 密码学代码复写 本学期我们学习了现代密码学这门课程,在上课的时候接触到了很多种类型的密码体制,对于一些典型很通用的密码体制有自己的学习和设计.不论是从密码体制还 ...

  9. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

随机推荐

  1. redis命令集合

    一.连接控制 QUIT 关闭连接 AUTH (仅限启用时)简单的密码验证 二.适合全体类型的命令 EXISTS key 判断一个键是否存在;存在返回 1;否则返回0;DEL key 删除某个key,或 ...

  2. 添加SSH密钥到GitHub

    $ clip < ~/.ssh/id_rsa.pubbash: /c/Users/UsersName/.ssh/id_rsa.pub: No such file or directory [转] ...

  3. cf(#div1 B. Dreamoon and Sets)(数论)

    B. Dreamoon and Sets time limit per test 1 second memory limit per test 256 megabytes input standard ...

  4. hdu----(4301)Divide Chocolate(状态打表)

    多校综合排名前25名的学校请发送邮件到HDUACM@QQ.COM,告知转账信息(支付宝或者卡号) Divide Chocolate Time Limit: 2000/1000 MS (Java/Oth ...

  5. 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  6. 判断jQuery元素是否隐藏

    第一种:使用CSS属性 复制代码 代码如下: var display =$('#id').css('display'); if(display == 'none'){    alert("被 ...

  7. php-fpm的重启/关闭

    php 5.3.3 下的php-fpm 不再支持 php-fpm 以前具有的 /usr/local/php/sbin/php-fpm (start|stop|reload)等命令,需要使用信号控制: ...

  8. 有一种感动叫ACM(记WJMZBMR在成都赛区开幕式上的讲话)

    各位选手,各位教练,大家好,我是来自清华大学交叉信息学院的陈立杰,今天很荣幸站在这里代表全体参赛选手发言.对于我来说,这是我第一次正式参加ACM的比赛.不过我跟ACM之间的缘分,大概在很早的时候就已经 ...

  9. HDU4815

    Little Tiger vs. Deep Monkey Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K ( ...

  10. 10个必备的移动UI设计资源站(转)

    创建移动设计模式是非常重要的一步,记住!这是为移动设备设计而不是web.不仅仅是移动屏幕远小于普通的电脑屏幕,关键是鼠标和键盘已经被手指替代了! 当然还有更重要的,说起来很苦逼,我们再也不能使用一种模 ...