自定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. iOS CoreData 开发之数据模型关系

    接着上一篇,上一篇中,我们简单的实现了一个用户实体,本次添加一个用户信息实体,与用户实体相关联,关系为1:1. 新建一个实体UserInfo:

  2. 【转】升级还是权谋?从USB PD 2.0到3.0

    原文出处 http://www.eetop.cn/blog/html/43/n-433743.html 如同iPhone的出现,才让智能机真正主导手机市场一样,Type-C口发布后,USB PD才正式 ...

  3. 移动web——touch事件介绍

    基本概念 1.在移动web端点击事件或者滑动屏幕.捏合等动作都是由touchstar.touchmove.touchend这三个事件组合在一起使用的 2.click事件在移动端会有0.2秒的延迟,下面 ...

  4. 六时出行 App iOS隐私政策

    本应用尊重并保护所有使用服务用户的个人隐私权.为了给您提供更准确.更有个性化的服务,本应用会按照本隐私权政策的规定使用和披露您的个人信息.但本应用将以高度的勤勉.审慎义务对待这些信息.除本隐私权政策另 ...

  5. [Windows Server 2008] 查看ASP.net详细错误信息

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com ★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频. ★ 本节我们将带领大家:查看IIS ...

  6. VC++文件监控 ReadDirectoryChangesW

    #include <windows.h> #include <tchar.h> #include <stdio.h> #include <assert.h&g ...

  7. Ubuntu无线转有线教程

    本来想测试一下有线转无线的,奈何网卡不支持,所以就测试了一回无线转有线的测试!(真无聊,不过也算学习一下linux网桥的知识) ca0gu0@ub:~$ sudo brctl addbr br0 #添 ...

  8. PAT-day1

    1001 害死人不偿命的(3n+1)猜想 (15 分)   卡拉兹(Callatz)猜想: 对任何一个正整数 n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把 ( 3n+1)砍掉一半.这样一直 ...

  9. day41 网络编程

    目录 网络架构 单机架构 CS架构 BS架构 互联网和互联网的组成(教材版) 边缘部分: 核心部分: 互联网的组成 硬件 软件 打开网页的过程(科普版) 物理层 数据链路层 网络层 传输层 抽象层 网 ...

  10. 数据结构与算法(6) -- heap

    binary heap就是一种complete binary tree(完全二叉树).也就是说,整棵binary tree除了最底层的叶节点之外,都是满的.而最底层的叶节点由左至右又不得有空隙. 以上 ...