一:基础部分

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. Directory.GetCurrentDirectory和Application.StartupPath的区别

    System.IO.Directory.GetCurrentDirectory()方法用于获得应用程序当前工作目录.System.Windows.Forms.Application.StartupPa ...

  2. Innodb中的事务隔离级别和锁的关系

    前言: 我们都知道事务的几种性质,数据库为了维护这些性质,尤其是一致性和隔离性,一般使用加锁这种方式.同时数据库又是个高并发的应用,同一时间会有大量的并发访问,如果加锁过度,会极大的降低并发处理能力. ...

  3. sh脚本异常:bad interpreter: No such file or directory

    转:http://bluedest.iteye.com/blog/1674963 在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file o ...

  4. 初学java之StringBuffer类的常用方法

    import java.text.*; public class Gxjun { public static void main(String atgs[]) { StringBuffer str= ...

  5. LOOPS(HDU 3853)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Sub ...

  6. java json 的生成和解析 --json-lib

    类(java json的解析和生成): import java.util.HashMap; import java.util.Map; import net.sf.json.JSONArray; im ...

  7. php include

    get_include_path  获取当前 include_path 配置选项的值,在当前代码目录未找到include文件时,则到include_path去include. set_include_ ...

  8. UVALive 7297 Hounded by Indecision BFS

    题目链接:Hounded by Indecision 题意:map中给出小偷的位置,警察的位置.警察有一只狗,开始的时候警察和狗一起行动,也就是看做一个格子,当警察遇见小偷走过的格子时,狗就会嗅到它的 ...

  9. 如何给一个网卡配置多个虚拟ip

    1.执行命令 ifconfig etho: 192.168.1.101 netmask 255.255.255.0 up 2.要想永久保存,则将刚刚那行代码写入/etc/rc.local  (开机都会 ...

  10. spark 学习

    三种编译方式 1. 编译文档:more—>buiding spark 2. 三种编译方式:SBT,Maven,打包编译 make-distribution.sh 运行方式 local,stand ...