为什么80%的码农都做不了架构师?>>>   

一 UITableView简介

UITableView用于表格数据展示,是使用频率最高的控件。UITableView继承自UIScrollView,具有UIScrollView的所有特性。

二 UITableView的两种样式

2.1 UITableViewStylePlain                                   2.2 UITableViewStyleGrouped

                     

三 UITableView数据展示

UITableView需要数据源(dataSource)来显示数据,实现UITableViewDataSource协议的对象,都可以是UITableView的数据源。通过协议中的方法,UITableView显示相应的行数以及每行显示的数据。

3.1 调用数据源的以下方法获取一共有多少组数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
3.2 调用数据源的以下方法获取每一组有多少行数据
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
3.3 调用数据源的以下方法获取每一行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

四 UITableViewCell

UITableView每行都是一个UITableViewCell,通过dataSource的tableView:cellForRowAtIndexPath:方法来初始化每行

UITableViewCell内部有个默认的子视图:contentView,contentView是UITableViewCell所显示内容的父视图,可以通过设置UITableViewCell的accessoryType来显示一些辅助指示视图,辅助指示视图用于表示动作的图标。

4.1 辅助指示视图
UITableViewCell的accessoryType值 图标样式
UITableViewCellAccessoryNone(默认) 不显示辅助指示视图
UITableViewCellAccessoryDisclosureIndicator
UITableViewCellAccessoryDetailDisclosureButton
UITableViewCellAccessoryCheckmark
UITableViewCellAccessoryDetailButton

注:还可以通过UITableViewCell的accessoryView属性来自定义辅助指示视图

4.2 contentView 的子视图
//4.2.1 textLabel
@property (nonatomic, readonly, retain) UILabel *textLabel;//4.2.2 detailTextLabel
@property (nonatomic, readonly, retain) UILabel *detailTextLabel;//4.2.3 imageView
@property (nonatomic, readonly, retain) UIImageView *imageView;
4.3 UITableViewCell的UITableViewCellStyle属性

UITableViewCellStyle用于决定使用contentView的哪些子视图,以及这些子视图在contentView中的位置

UITableViewCellStyle的值 子视图样式
UITableViewCellStyleDefault
UITableViewCellStyleSubtitle
UITableViewCellStyleValue1
UITableViewCellStyleValue2

五 界面简单实现

提供car类,创建car类的数组,用于展示.

5.1 代码
--car类
@interface GMCar : NSObject
@property (nonatomic, copy) NSString   *title;
@property (nonatomic, copy) NSString   *desc;
@property (nonatomic, strong) NSArray *subCars;
@end
#import "ViewController.h"
#import "GMCar.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *array;
@end@implementation ViewController- (void)viewDidLoad {//1.UITableView 设置//1.1 设置数据代理self.tableView.dataSource = self;//1.2 设置frameself.tableView.frame = self.view.frame;//1.3 设置代理self.tableView.delegate = self;//2.UITableView加入veiw[self.view addSubview:self.tableView];
}#pragma mark - 数据加载
/***  调用数据源以下方法获取一共有多少组数据**  @param tableView <#tableView description#>**  @return <#return value description#>*/
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{return self.array.count;
}/***  调用数据源方法获取每一组有多少行数据**  @param tableView <#tableView description#>*  @param section   <#section description#>**  @return <#return value description#>*/
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.subCars.count;
}/***  调用数据源方法获取每一行显示什么内容**  @param tableView <#tableView description#>*  @param indexPath <#indexPath description#>**  @return <#return value description#>*/
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{//1.创建cellUITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];//1.1 设置accessoryType//    UITableViewCellAccessoryNone,                   // don't show any accessory view//    UITableViewCellAccessoryDisclosureIndicator,    // regular chevron. doesn't track//    UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks//    UITableViewCellAccessoryCheckmark,              // checkmark. doesn't track//    UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. trackscell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;//2.取出数据GMCar *car = self.array[indexPath.section];NSString *name =car.subCars[indexPath.row];//3.设置数据cell.textLabel.text =name;return cell;
}/***  section组显示标题**  @param tableView <#tableView description#>*  @param section   <#section description#>**  @return <#return value description#>*/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.title;
}/***  section组显示描述**  @param tableView <#tableView description#>*  @param section   <#section description#>**  @return <#return value description#>*/
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{GMCar *car = self.array[section];return car.desc;
}#pragma mark - UITableViewDelegate
/***  table 每一行的高度**  @param tableView <#tableView description#>*  @param indexPath <#indexPath description#>**  @return <#return value description#>*/
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{return 20.0;
}/***  section组Header的高度**  @param tableView <#tableView description#>*  @param section   <#section description#>**  @return <#return value description#>*/
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{NSLog(@"height");return 30.0;
}/***  section组Footer的高度**  @param tableView <#tableView description#>*  @param section   <#section description#>**  @return <#return value description#>*/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{return 30.0;
}#pragma mark - 懒加载
-(UITableView *)tableView
{if (!_tableView) {//UITableViewStyleGrouped样式_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];//UITableViewStylePlain 样式//_tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];}return _tableView;
}-(NSArray *)array
{if (!_array) {_array = [[NSArray alloc]init];GMCar *c1 = [[GMCar alloc] init];c1.title = @"德国品牌";c1.desc = @"世界一流品牌";c1.subCars = @[@"奥迪" , @"宝马"];GMCar *c2 = [[GMCar alloc] init];c2.title = @"日本品牌";c2.desc = @"实用价值高";c2.subCars = @[@"丰田" , @"本田"];GMCar *c3 = [[GMCar alloc] init];c3.title = @"欧美品牌";c3.desc = @"高逼格";c3.subCars = @[@"劳斯莱斯" , @"布加迪", @"兰博基尼"];_array = @[c1, c3, c2];}return _array;
}#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{return YES;
}
@end
5.2 展示

UITableViewStylePlain 样式                      UITableViewStyleGrouped 样式

       

UITableViewCell的accessoryType值为UITableViewCellAccessoryDetailDisclosureButton

转载于:https://my.oschina.net/wolx/blog/366709

IOS UITableView详解一数据展示 页面简单实现相关推荐

  1. IOS UITableView详解二性能优化 LOL游戏人物展示

    为什么80%的码农都做不了架构师?>>>    一 重用UITableViewCell UITableView滑动过程中,屏幕底部的信息上移到屏幕,会创建UITableViewCel ...

  2. iOS绘图详解-多种绘图方式、裁剪、滤镜、移动、CTM

    iOS绘图详解 摘要: Core Graphics Framework是一套基于C的API框架,使用了Quartz作为绘图引擎.它提供了低级别.轻量级.高保真度的2D渲染.该框架可以用于基于路径的 绘 ...

  3. IOS UIView详解

    文章目录 IOS UIView详解 1.官方类分析 2. UIView 常用的属性 2.1 UIView的圆角加阴影效果的实现 2.2 UIView 属性 2.2.1 UIView 几何属性 2.2. ...

  4. vue 加载页面时触发时间_详解Vue.js在页面加载时执行某个方法

    详解Vue.js在页面加载时执行某个方法 jQuery中可以这样写 vue中,如果要达到相同效果,可以使用vue的生命周期函数,如create或者mounted 附上vue.js的生命周期函数执行流程 ...

  5. iOS多线程详解:实践篇

    iOS多线程实践中,常用的就是子线程执行耗时操作,然后回到主线程刷新UI.在iOS中每个进程启动后都会建立一个主线程(UI线程),这个线程是其他线程的父线程.由于在iOS中除了主线程,其他子线程是独立 ...

  6. iOS疯狂详解之开源库

    youtube下载神器:https://github.com/rg3/youtube-dl vim插件:https://github.com/Valloric/YouCompleteMe vim插件配 ...

  7. 详解微信小程序页面间传递信息的三种方式

    详解微信小程序页面间传递信息的三种方式 在开发微信小程序的时候,经常会遇到在页面间传递信息的情况,有三种方法可以实现. 1. 使用数据缓存 将要存储的数据使用以下方法放入缓存 wx.setStorag ...

  8. R语言tidyr包gather()函数实战详解:数据收缩、从宽表到窄表

    R语言tidyr包gather()函数实战详解:数据收缩.从宽表到窄表 目录 R语言tidyr包gather()函数实战详解:数据收缩.从宽表到窄表 收缩两列数据

  9. R语言tidyr包spread()函数实战详解:数据裂变、从窄表到宽表

    R语言tidyr包spread()函数实战详解:数据裂变.从窄表到宽表 目录 R语言tidyr包spread()函数实战详解:数据裂变.从窄表到宽表

最新文章

  1. Xubunbtu远程桌面的tab键
  2. Android 平台的Crash崩溃捕获-全
  3. 【模拟】Biotech
  4. spring mvc学习(21):testparam请求参数和请求头表达式
  5. jqGrid方法整理
  6. 手把手玩转win8开发系列课程(22)
  7. PRML_4章 线性模型分类笔记
  8. 劣币驱逐良币,人吃人的中国职场环境还能走多远
  9. linux泰语语言包,linux安装中文语言包(示例代码)
  10. 她笔下的水墨世界令人赞叹,中国风海报沉浸式国风体验
  11. win7 x64下安装python-opencv 及 “not a supported wheel”解决
  12. 201819102040张辰飞
  13. Coherence X for mac(网站转换为mac应用的工具)
  14. 【AutoSAR】【MCAL】PWM
  15. 第九天 (集合 ArrayList)
  16. 自动化测试的理解总结与感悟
  17. 华为Atlas张迪煊:在最好的时代,做最强AI算力底座
  18. 鲲鹏服务器和英特尔服务器性能,华为服务器用的鲲鹏CPU与英特尔至强CPU,性能到底有多大差距?...
  19. Python 爬虫 bs4 数据解析基本使用
  20. 解决错误Annotation processors must be explicitly declared now

热门文章

  1. MySql库中所有表的属性_SQL查询某库所有的表所有的字段及字段的属性
  2. 适合win7的python版本_windows下多个python版本共存,如何在Windows7系统上安装最新的64位Python3.6.2...
  3. Java项目:网上书城+后台管理系统(java+jsp+servlert+mysql+ajax)
  4. php mysql环境搭配_centos6.7下搭配apache php mysql环境
  5. 微信小程序用户未授权bug解决方法,微信小程序获取用户信息失败解决方法
  6. 15-flutter Scaffold详解
  7. Quartz2D在项目中的实际使用
  8. Centos 7使用vsftpd搭建FTP服务器
  9. Openfire服务器的安装部署
  10. spring @component的作用