自定cell(XIB)团购思路

步骤一、先解析plist文件,创建model层数据。

- (instancetype)initWithDict:(NSDictionary
*)dict

{

   
self = [super
init];

   
if (self) {

        [self
setValuesForKeysWithDictionary:dict];

    }

   
return
self;

}



+ (instancetype)tgWithDict:(NSDictionary
*)dict

{

   
return [[self
alloc]
initWithDict:dict];

}



+ (NSMutableArray
*)tgs

{

   
NSArray *array = [NSArray
arrayWithContentsOfFile:[[NSBundle
mainBundle]
pathForResource:@"tgs.plist"
ofType:nil]];

   

   
NSMutableArray *arrayM = [NSMutableArray
array];

   
for (NSDictionary
*dict
in array) {

        [arrayM
addObject:[self
tgWithDict:dict]];

    }

   

   
return arrayM;

}
二、自己定义Cell
1> 
创建XIB文件,并设置控件内部细节。

2> 
创建一个与XIB同名的类,注意自己定义的类继承自的对象,要与XIB中根对象的类保持一致。

3> 
又一次指定XIB中的根类,打开助理编辑器辅助观察变化。

4> 
对须要改动属性的控件做IBOutlet连线,注意,不要与TableViewCell的已经存在的属性重名

连线控件的名称不能是:

(1) textLabel

(2) detailTextLabel

(3) imageView


+ (instancetype)cellWithTableView:(UITableView
*)tableView

{

   
// 1.
可重用标示符

   
static
NSString *ID =
@"Cell";

   
// 2. tableView查询可重用Cell

   
HMTgCell *cell = [tableView
dequeueReusableCellWithIdentifier:ID];

   

   
// 3.
假设没有可重用cell

   
if (cell ==

nil) {

       
NSLog(@"载入XIB");

       
//
从XIB载入自己定义视图

        cell = [[[NSBundle
mainBundle]
loadNibNamed:@"HMTgCell"
owner:nil
options:nil]
lastObject];

    }

   

   
return cell;

}



- (void)setTg:(HMTg
*)tg

{

   
// setter方法中,第一句要赋值,否则要在其它方法中使用模型,将无法訪问到

   
_tg = tg;



   
self.titleLabel.text
= tg.title;

   
self.iconView.image
= [UIImage
imageNamed:tg.icon];

   
self.priceLabel.text
= tg.price;

   
self.buyCountLabel.text
= tg.buyCount;

}



#pragma mark -
模板提供的方法

/**

 初始化方法

 

 使用代码创建Cell的时候会被调用,假设使用XIB或者Storyboard。此方法不会被调用

 */

- (id)initWithStyle:(UITableViewCellStyle)style
reuseIdentifier:(NSString
*)reuseIdentifier

{

   
self = [super
initWithStyle:style
reuseIdentifier:reuseIdentifier];

   
if (self) {

       
NSLog(@"%s", __func__);

    }

   
return
self;

}



/**

 从XIB被载入之后,会自己主动被调用,假设使用纯代码。不会被运行

 */

- (void)awakeFromNib

{

   
NSLog(@"%s", __func__);

   
self.contentView.backgroundColor
= [UIColor
clearColor];

}



/**

 Cell
被选中或者取消选中是都会被调用

 

 假设是自己定义Cell控件,全部的子控件都应该加入到contentView中

 */

- (void)setSelected:(BOOL)selected
animated:(BOOL)animated

{

    [super
setSelected:selected
animated:animated];



   
if (selected) {

       
self.contentView.backgroundColor
= [UIColor
redColor];

    }
else {

       
self.contentView.backgroundColor
= [UIColor
greenColor];

    }

}

三、在控制器实现UITableView的代理方法
#pragma mark -
数据源方法

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

{

   
return
self.tgs.count;

}



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

{

   
// 1.
创建cell

   
HMTgCell *cell = [HMTgCell
cellWithTableView:tableView];

   

   
// 2.
通过数据模型。设置Cell内容,能够让视图控制器不须要了解cell内部的实现细节

    cell.tg
=
self.tgs[indexPath.row];

   

   
return cell;

}
四、自己定义点击開始下载XIB
+ (instancetype)footerView

{

   
return [[[NSBundle
mainBundle]
loadNibNamed:@"HMTgFooterView"
owner:nil
options:nil]
lastObject];

}



- (IBAction)loadMore

{

   
NSLog(@"载入很多其它");

   
// 1.
隐藏button

   
self.loadMoreButton.hidden
=
YES;

   
// 2.
显示提示视图

   
self.tipsView.hidden
=
NO;



   
// 3.1
推断代理是否实现了协议方法

   
if ([self.delegate
respondsToSelector:@selector(tgFooterViewDidDownloadButtonClick:)])
{

        [self.delegate
tgFooterViewDidDownloadButtonClick:self];

    }

}

/**
视图控制器刷新完毕调用方法
*/
在XIB加入View和Button,载入完数据之后,View隐藏,Button显示。

- (void)endRefresh

{

   
// 4.
载入完毕数据

   
self.loadMoreButton.hidden
=
NO;

   
self.tipsView.hidden
=
YES;

}

五、自己定义协议,当点击開始下载button,通知控制器
@protocol
HMTgFooterViewDelegate <NSObject>



@optional

/**
视图的下载button被点击
*/

- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView
*)footerView;



@end

- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView
*)footerView

{

   
//
模拟取网络上获取数据载入数据

   
NSLog(@"努力载入数据中....");

   

   
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,
(int64_t)(2.0
*
NSEC_PER_SEC)),
dispatch_get_main_queue(), ^{

       
//
获得网络数据之后运行的操作

       

       
//
向数组中加入数据,模拟网络载入完毕之后的效果

       
NSDictionary *dict =
@{@"title":
@"哈哈",
@"icon":
@"ad_00",
@"price":
@"100.2",
@"buyCount":
@"250"};

       
HMTg *tg = [HMTg
tgWithDict:dict];

       

       
NSLog(@"加数据前
%d",

self.tgs.count);

       

        [self.tgs
addObject:tg];

       

       
NSLog(@"加数据后
%d",

self.tgs.count);

       
//
刷新数据

       
//    [self.tableView reloadData];

       
//
新建一个indexPath

       
NSIndexPath *path = [NSIndexPath
indexPathForRow:(self.tgs.count
-
1)
inSection:0];

        [self.tableView
insertRowsAtIndexPaths:@[path]
withRowAnimation:UITableViewRowAnimationMiddle];

       

       
//
通知页脚视图调整视图显示状态

        [footerView
endRefresh];

    });

}

自定cell(XIB)团购思路的更多相关文章

  1. AJ学IOS(16)UI之XIB自定义Cell实现团购UI

    AJ分享,必须精品 先看效果图 自定义Cell 本次主要是自定义Cell的学习 实现自定义Cell主要有三种方法:按照使用的频繁度排序: XIB > 纯代码 > StoryBoard XI ...

  2. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例

    一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...

  3. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  4. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  5. [iOS基础控件 - 6.6.1] 展示团购数据代码

      1.主控制器: // // ViewController.m // GroupPurchase // // Created by hellovoidworld on 14/12/3. // Cop ...

  6. [iOS基础控件 - 6.6] 展示团购数据 自定义TableViewCell

    A.需求 1.头部广告 2.自定义cell:含有图片.名称.购买数量.价格 3.使用xib设计自定义cell,自定义cell继承自UITableViewCell 4.尾部“加载更多按钮”,以及其被点击 ...

  7. iOS UI基础-9.1 UITableView 团购

    概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图      团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...

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

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

  9. swift项目-模仿团购(主界面的搭建,以及首页的一些细节)

    以前学习oc的时候写的一个团购的项目,现在学习swift,拿来用swift写一遍,也是连猜带蒙的,一点一点的往上凑. 今天主要是把主要的架子搭起来了. 主要有:UITabBarController,U ...

随机推荐

  1. Spring Cloud (6) 分布式配置中心-高可用

    高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...

  2. Hadoop基础(二)

    HDFS 读写流程 我们知道在HDFS中我们的文件按数据块进行存储,那么当我们写入或者读取一个文件的时候HDFS到底进行了哪些操作呢? HDFS 写流程 如上图所示,假如我们有一个四个节点的集群,并且 ...

  3. MyBatis分页组件--PageHelper

    一.介绍 PageHelper是国内非常优秀的一款开源的 mybatis 分页插件,它支持基本主流与常用的数据库,例如 Oracle.Mysql.MariaDB.SQLite.Hsqldb 等. 官网 ...

  4. 五分钟学习React(五):React两种构建应用方式选择

    经过这四期的讲解,我们从Hello World应用入手,解释了React最重要的概念JSX,以及两种不同模式的应用构建方法.这一讲我们着重对比传统模式和新模式下的React项目构建,从而为初学者提供学 ...

  5. 【技术累积】【点】【java】【25】Orderd

    基础概念 Orderd是spring core中定义的一个接口,使用它以及相关的Comparator和@Order注解,可以实现对元素的排序. @Order 直接先说下@Order注解吧,使用场景较多 ...

  6. PHP 之转换excel表格中的经纬度

    <?php set_time_limit(0); include './plugin/PHPExcel/PHPExcel.php'; include './plugin/PHPExcel/PHP ...

  7. cocos自动图集

    对于图像资源,为什么要用图集,cocos官网的解释: 1.合成图集时会去除每张图片周围的空白区域,加上可以在整体上实施各种优化算法,合成图集后可以大大减少游戏包体和内存占用2.多个 Sprite 如果 ...

  8. CAD插入jpg

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. servlet之@PostConstruct,@PreDestroy

    1.@PostConstruct说明 被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法.被@PostCo ...

  10. 洛谷——P1572 计算分数

    P1572 计算分数 模拟+字符串 注意有两位数的情况以及负数情况 #include<bits/stdc++.h> using namespace std; string s; ],b[] ...