UITableView有两种风格:UITableViewStylePlain和UITableViewStyleGrouped。这两者操作起来其实并没有本质区别,只是后者按分组样式显示前者按照普通样式显示而已。大家先看一下两者的应用

1、首先我们介绍一下平铺的tableView,初始化一个tableView如下

#pragma mark - 设置子视图
- (void)setSubviews{ UITableView * table=[[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height-) style:UITableViewStylePlain];
self.tableView=table; //设置数据源代理
table.dataSource=self;
//设置方法属性代理
table.delegate=self; [self.view addSubview:table]; }

tableView需要设置两个代理,而要作为tableView的代理必须实现其代理方法,并遵守协议

/**
tableView 代理功能:
1 需要告知展示数据的条数
2 需要告知展示的内容 要想作为tableView的代理 需要遵守UITableViewDataSource 和 UITableViewDelegate两个协议
*/ @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> /** 数据数组*/
@property(nonatomic,strong) NSArray * dataArray; /** tableView接口*/
@property(nonatomic,weak) UITableView * tableView; @end

2、我们通过当前的系统字体作为显示的内容,加载数据如下

#pragma mark - 加载数据
- (void)loadData{
self.dataArray=[UIFont familyNames]; }

3、实现代理方法

#pragma mark - UITableViewDatasSource

//返回记录条数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.dataArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 重复利用标识
NSString * identy=@"JRCell"; //从缓冲池获取可以利用的cell
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy]; //如果缓冲池没有可利用对象需要重新创建
if (cell==nil) {
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy]; cell.selectionStyle=UITableViewCellSelectionStyleNone; } cell.textLabel.text=self.dataArray[indexPath.row]; cell.textLabel.font=[UIFont fontWithName:self.dataArray[indexPath.row] size:]; return cell;
}

4、效果图如下

  5、最简单的tableView 我们就做完了,但是在日常开发中,我们需要用到的功能不仅仅这么简单,有的时候cell是需要自定义的,下面我们完成一个美团列表展示自己定义的tableView

6、这里要求我们自定义美团列表cell,对于数据的加载和读取这里不做介绍,我们把重点放在如何自定义cell上面

① 我们先自定义一个cell,并且继承了UITableViewCell

② 然后我们向当前cell中拼接子视图

#pragma mark - 增加子视图
- (void) setSubview{ // 1 增加图标
UIImageView * jrImageView=[[UIImageView alloc] initWithFrame:CGRectMake(, , kRowHeight-, kRowHeight-)];
self.jrImageView=jrImageView;
jrImageView.backgroundColor=[UIColor redColor];
[self.contentView addSubview:jrImageView]; // 2 增加标题
UILabel * titleLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+, , kWidth-CGRectGetMaxX(jrImageView.frame)-, )];
titleLable.text=@"汉金城烤肉自助餐厅";
titleLable.font=[UIFont boldSystemFontOfSize:];
self.jrTitleLable=titleLable;
[self.contentView addSubview:titleLable]; // 3 增加子标题
UILabel * detailLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+, CGRectGetMaxY(titleLable.frame), kWidth-CGRectGetMaxX(jrImageView.frame)-, )];
detailLable.text=@"汉金城烤肉自助餐厅汉金城烤肉自助餐厅汉金";
detailLable.numberOfLines=;
detailLable.font=[UIFont boldSystemFontOfSize:];
detailLable.textColor=[UIColor grayColor];
self.jrDetailLable=detailLable;
[self.contentView addSubview:detailLable]; // 4 增加价格
UILabel * priceLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(jrImageView.frame)+, CGRectGetMaxY(detailLable.frame), , )];
priceLable.text=@"$2.9";
priceLable.font=[UIFont boldSystemFontOfSize:];
priceLable.textColor=[UIColor colorWithRed:/255.0 green:/255.0 blue:/255.0 alpha:];
self.jrPriceLale=priceLable;
[self.contentView addSubview:priceLable]; // 5 已售数量
UILabel * sellLable=[[UILabel alloc] initWithFrame:CGRectMake(kWidth-, CGRectGetMaxY(detailLable.frame), , )];
sellLable.text=@"已售1150";
sellLable.font=[UIFont boldSystemFontOfSize:];
sellLable.textColor=[UIColor grayColor];
self.jrSellLable=sellLable; [self.contentView addSubview:sellLable]; }

③ 我们需要对子视图开辟接口出来让外界访问

④ 定义方法初始化数据

#pragma mark - 初始化数据
- (void) initDataWithInfo:(Information *) info{ //设置图标
self.jrImageView.image=[UIImage imageNamed:info.strPic]; //设置标题
self.jrTitleLable.text=info.title; //设置明细
self.jrDetailLable.text=info.detailTitle; //设置价格
self.jrPriceLale.text=[NSString stringWithFormat:@"$%.1f",info.price]; //设置销量
self.jrSellLable.text=[NSString stringWithFormat:@"销量%ld",info.soldNum]; }

⑤ 我们在代理方法里面,初始化我们自定义cell并且设置数据即可

#pragma mark 返回cell
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString * identy=@"jrCell"; JRTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:identy]; if (cell==nil) { cell=[[JRTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identy]; //设置选中样式为空
cell.selectionStyle=UITableViewCellSelectionStyleNone; } //重新设置数据
Information *info= self.dataArray[indexPath.row];
[cell initDataWithInfo:info]; return cell; }
作者:杰瑞教育
出处:http://www.cnblogs.com/jerehedu/ 
版权声明:本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

技术咨询:
 

IOS之导航控制器传值的更多相关文章

  1. iOS:导航控制器侧滑出栈实现

    介绍:在iOS中,导航控制器UINavigationController是默认实现左侧边缘侧滑手势出栈的,但是如果当开发者对导航控制器子控制实现自定义leftBaButtonItem时,这个侧滑功能就 ...

  2. IOS UINavigationController 导航控制器

    /** 导航控制器掌握: 1.创建导航控制器 UINavigationController *nav = [[UINavigationController alloc] initWithRootVie ...

  3. IOS中导航控制器的代理及隐藏控制器刚出现时的滚动条

    一.导航控制器的代理 1.UINavigationController的delegate属性 2.代理方法 1> 即将显示新控制器时调用 /* navigationController : 导航 ...

  4. IOS之导航控制器

    UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图.任何类型的视图控制器都可放入栈中.在设计导航控制器时需要指定根视图即用户看到的第一个视图.根视图 ...

  5. iOS结合导航控制器和标签栏控制器

    <span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name=& ...

  6. iOS 隔离导航控制器

    题外话:最近这两个月一直很闲,项目上基本没有啥大的需求.对于程序员来说,如果没有需求其实是一件很难受的事情,之前好多次在项目中没事找事,该优化的优化,该整理的整理.可能好多程序员都遇到过与我类似的情况 ...

  7. iOS开发 — (UINaVigationController)导航控制器,界面传值

    UINavigationController 继承于 UIViewController , 以栈的方式管理所 控制的视图控制器 . 至少要有一个被管理的视图控制器 ,这个控制器我们称作导航控制器的根视 ...

  8. 利用协议代理实现导航控制器UINavigationController视图之间的正向传值和反向传值

    实验说明 (1)正向传值:比如A类里地值要传给B类用,就是我们先在A类中声明一个B类对象(当然B类头文件要import过来),然后把A类中得某个 值传递给B类中得某个值(所以需要在B类中先准备一个变量 ...

  9. IOS初级:导航控制器

    1.AppDelegate.m老生常谈了,创建window,创建根视图rootViewController - (BOOL)application:(UIApplication *)applicati ...

随机推荐

  1. Stream中reduce()使用记录

    一.reduce()使用1.第一个参数是我们给出的初值,2.第二个参数是累加器,可以自己用实现接口完成想要的操作,这里使用Bigdecimal的add方法 3.最后reduce会返回计算后的结果 Bi ...

  2. CSS基础-DAY2

    CSS属性操作-文本 文本颜色 <head> <style> p{ /*color:#8B5742 ;色码表*/ color: RGBA(255,0,0,0.5); /*调色, ...

  3. 使用gdb调试

    启用gdb进行调试二进制程序,必须在二进制程序在采用gcc或g++编译时加入-g参数 启动gdb进行调试的几种形式: 直接启动gdb程序进行调试program程序 gdb program 启动gdb挂 ...

  4. dSploitzANTI渗透教程之安装zANTI工具

    dSploitzANTI渗透教程之安装zANTI工具 Dsploit/zANTI基础知识 zANTI是一款Android平台下的渗透测试工具,支持嗅探已连接的网络.支持中间人攻击测试.端口扫描.Coo ...

  5. Codeforces.724G.Xor-matic Number of the Graph(线性基)

    题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...

  6. hihocoder 1866 XOR

    题面在这里 拆位分析一下就OK啦 /* y + (y xor x) */ #include<bits/stdc++.h> #define ll long long using namesp ...

  7. [CF183D]T-shirt

    [CF183D]T-shirt 题目大意: 有\(n(n\le3000)\)个人和\(m(m\le300)\)种T恤,每个人都有一种喜欢的T恤,你知道每个人喜欢每种T恤的概率\(p_{i,j}\). ...

  8. poj 1062 昂贵的聘礼 最短路 dijkstra

    #include <cstdio> #include <cmath> #include <cstring> #include <ctime> #incl ...

  9. Git_配置别名

    有没有经常敲错命令?比如git status?status这个单词真心不好记. 如果敲git st就表示git status那就简单多了,当然这种偷懒的办法我们是极力赞成的. 我们只需要敲一行命令,告 ...

  10. javascript中Date对象复习

    js的Date对象不怎么经常用,所以忘得差不多,复习一下 1.声明一个Date对象,默认本地当前时间 var date = new Date();//Fri Apr 28 2017 14:26:19 ...