• 设置数据源对象
self.tableView.dataSource = self;
  • 数据源对象要遵守协议
@interface ViewController () <UITableViewDataSource>

@end
  • 实现数据源方法
// 多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // 每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // 每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; // 每一组的头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // 每一组的尾部
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section

tableView的常见设置

// 设置每一行cell的高度
self.tableView.rowHeight = 100; // 设置每一组头部的高度
self.tableView.sectionHeaderHeight = 50; // 设置每一组尾部的高度
self.tableView.sectionFooterHeight = 50; // 设置分割线颜色
self.tableView.separatorColor = [UIColor redColor];
// 设置分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// 设置表头控件
self.tableView.tableHeaderView = [[UISwitch alloc] init];
// 设置表尾控件
self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd]; // 设置右边索引文字的颜色
self.tableView.sectionIndexColor = [UIColor redColor];
// 设置右边索引文字的背景色
self.tableView.sectionIndexBackgroundColor = [UIColor blackColor];

tableViewCell的常见设置

// 设置右边的指示样式
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 设置右边的指示控件
cell.accessoryView = [[UISwitch alloc] init]; // 设置cell的选中样式
cell.selectionStyle = UITableViewCellSelectionStyleNone;
// backgroundView优先级 > backgroundColor // 设置背景色
cell.backgroundColor = [UIColor redColor]; // 设置背景view
UIView *bg = [[UIView alloc] init];
bg.backgroundColor = [UIColor blueColor];
cell.backgroundView = bg; // 设置选中的背景view
UIView *selectedBg = [[UIView alloc] init];
selectedBg.backgroundColor = [UIColor purpleColor];
cell.selectedBackgroundView = selectedBg;

cell的循环利用

  • 传统的写法
/**
* 每当有一个cell要进入视野范围内,就会调用一次
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"wine"; // 1.先去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果缓存池中没有可循环利用的cell
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} // 3.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row]; return cell;
}
  • 新的写法(注册cell)
NSString *ID = @"wine";

- (void)viewDidLoad {
[super viewDidLoad]; // 注册某个重用标识 对应的 Cell类型
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1.先去缓存池中查找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.设置数据
cell.textLabel.text = [NSString stringWithFormat:@"%zd行的数据", indexPath.row]; return cell;
}
 

如何让tableView展示数据的更多相关文章

  1. ios MVVM实践 刷新网络请求+tableView展示数据

    [实现效果] [目录结构相关] 此示例展示用的是MVVM结构形式,表述如下 M:数据Model的存储,可以用来对属性进行处理.(即胖model概念,上图中xx万人订阅这个处理方法写在Model内) V ...

  2. 使用UITableView展示数据

    TableView主要用于展示数据,类似于Android中的ListView. 我们可以通过两个方式使用TableView.第一种是直接使用TableView类.第二种是通过UITableViewCo ...

  3. iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)

    大致花了一个月时间,利用各种空闲时间,将这个客户端实现了,在这里主要是想记录下,设计的大体思路以及实现过程中遇到的坑...... 这个项目的github地址:https://github.com/wz ...

  4. PHP+Mysql+jQuery实现地图区域数据统计-展示数据

    我们要在地图上有限的区块内展示更多的信息,更好的办法是通过地图交互来实现.本文将给大家讲解通过鼠标滑动到地图指定省份区域,在弹出的提示框中显示对应省份的数据信息.适用于数据统计和地图区块展示等场景. ...

  5. C#-WinForm-ListView-表格式展示数据、如何将数据库中的数据展示到ListView中、如何对选中的项进行修改

    在展示数据库中不知道数量的数据时怎么展示最好呢?--表格 ListView - 表格形式展示数据 ListView 常用属性 HeaderStyle - "详细信息"视图中列标头的 ...

  6. Repeater控件 ---表格展示数据

    简介: Repeater控件是Web 服务器控件中的一个容器控件,它使您可以从页的任何可用数据中创建出自定义列表. Repeater 控件不具备内置的呈现功能,这表示用户必须通过创建模板为 Repea ...

  7. Windows程序==>>使用ListView控件展示数据

    使用ListView控件展示数据 01.ImageList控件 1.了解了解         属性 说明 Images 储存在图像列表中的所有图像 ImageSize 图像列表中图像的大小 Trans ...

  8. repeater灵活运用、repeater的commmand用法、如何不用repeater展示数据

    实体类: using System; using System.Collections.Generic; using System.Linq; using System.Web; /// <su ...

  9. Flex与Java交互(Flex调用java类展示数据)解析xml展示数据

    Flex与java通信最简单例子(详细说明了各种需要注意的配置):http://blog.csdn.net/u010011052/article/details/9116869 Flex与java通信 ...

随机推荐

  1. unix系统非roo账号安装JDK

    AIX系统用户rusky(非root用户,没有权限修改/etc/profile和/etc/environment文件 )直接解压JDK.zip文件,解压后把JAVA目录拷贝到/home/rusky目录 ...

  2. Android 启动Service服务和发送Broadcast广播的常用方法

    一.先说Service服务. 1.利用setAction()方法来指定启动的Service服务 Intent intent = new Intent(); intent.setAction(" ...

  3. Json.Net介绍及实例

    本系列教程假设读者已经对Json有一定的了解,关于Json在这里不多说.本系列教程希望能对读者开发涉及到Json的.Net项目有一定的帮助.本系列教程是根据官方文档资料和自己项目应用汇总而成.如果觉得 ...

  4. SonarQube 项目配置文件

    费话不说,直接上代码: 需要注意的地方: 1. 每个项目的key不能重复. 2. 注意编码方式. 3. 注意分模块的写法. 4. 忽略源码文件的写法. # Required metadatasonar ...

  5. 004 range的用法

  6. PHP基础之 file_get_contents() 函数

    定义和用法 file_get_contents() 函数把整个文件读入一个字符串中. 和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串. file_get ...

  7. HDU 5730 - Shell Necklace

    题意: 给出连续的1-n个珠子的涂色方法 a[i](1<=i<=n), 问长度为n的珠链共有多少种涂色方案 分析: 可以得到DP方程: DP[n] = ∑(i=1,n) (DP[n-i]* ...

  8. 从汇编看c++中指向成员变量的指针(一)

    在c++中,指向类成员变量的指针存储的并不是该成员变量所在内存的地址,而仅仅是该成员变量在该类对象中相对于对象首地址的偏移量.因此,它必须绑定到某一个对象或者对象指针上面,这里的对象和对象指针,就相当 ...

  9. oracle使用口令文件验证和os验证

    一.Oracle安装之后默认情况下是启用了OS认证的,这里提到的os认证是指服务器端os认证.OS认证的意思把登录数据库的用户和口令校验放在了操作系统一级.如果以安装Oracle时的用户登录OS,那么 ...

  10. AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表

    见归档项目ListViewDemo.zip. 基本步骤如下: 1.创建主布局文件,里面包含一个ListView元素. <RelativeLayout xmlns:android="ht ...