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

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

A.需求

1.头部广告

2.自定义cell:含有图片、名称、购买数量、价格

3.使用xib设计自定义cell,自定义cell继承自UITableViewCell

4.尾部“加载更多按钮”,以及其被点击之后的数据加载刷新、动画效果

code source: https://github.com/hellovoidworld/GroupPurchase

B.实现

1.使用MVC

M:团购Model

V:总View、cell的View(包含类和界面)

C:ViewController

2.分类管理代码文件

3.尾部footerView “加载更多"功能

1//设置尾部控件2self.tableView.tableFooterView = footerView;

footerView设置的按钮自动宽度为tableView宽度,只能设置高度;x和y也不能设置。

要自定义按钮的的,使用xib进行自定义footerView嵌套

(1)使用button, 设计一个“加载更多”按钮

(2)加上等待图标

—>如何利用xib封装一个View:

新建xib描述view的结构

新建一个继承UIView的类(取决于xib根对象的class)新建类名,和xib保持一致

@interface FooterRefreshView : UIView

设置xib控件的class,连线

在自定义class中定义xib的加载类方法(屏蔽xib加载过程)

1/** 初始化方法*/2+ (instancetype) footerRrefreshViewWithDelegate:(id<FooterRefreshViewDelegate>)delegate{3FooterRefreshView *footerRefreshView = [[[NSBundle mainBundle] loadNibNamed:@"FooterRefreshView"owner:nil options:nil] lastObject];45if(nil !=delegate) {6footerRefreshView.delegate=delegate;7}89returnfooterRefreshView;10}

class持有controller引用,发送消息给controller刷新数据

下面使用代理模式

—>改进:使用代理设计模式

自定义view的class持有controller的引用,耦合性强 —>使用代理协议命名规范:控件类名+Delegate代理方法普遍都是@optional代理对象遵守代理协议,实现代理协议里面的方法在需要的地方调用代理方法,给代理发送消息1FooterRefreshView.h2#import<UIKit/UIKit.h>34@classFooterRefreshView;56//定义delegate协议7@PRotocolFooterRefreshViewDelegate <NSObject>89@optional10- (void) footerRefreshViewClickedFooterRefreshButton:(FooterRefreshView *) footerRefreshView;1112@end1314@interfaceFooterRefreshView : UIView1516+ (instancetype) footerRrefreshViewWithDelegate:(id<FooterRefreshViewDelegate>)delegate;1718@end

4.xib中创建的view初始化完毕之后不会调用class中的init方法,而是调用awakeFromNib方法

FooterRefreshView.h

1//xib控件的初始化调用方法2- (void)awakeFromNib {3self.loadingImage.hidden =YES;4}

5.分割线

其实就是高度为1的UIView

6.自定义cell

(1).自定义cell的子控件都要放到contentView里面

默认就是会放到contentView中

(2)创建继承自UITableViewCell的类

@interface GroupPurchaseCell : UITableViewCell

(3)创建xib文件描述cell的界面

(4)指定view的class,对控件进行连线

(5)在cell类中,定义一个model成员,用来存储这个cell的数据

1/** 自定初始化的类方法,传入model数据*/2+ (instancetype) groupPurchaseCellWithGroupPurchase:(GroupPurchase *) groupPurchase;

(6)创建初始化方法,使用model数据作为传入参数

1/** 自定初始化的类方法,传入model数据*/2+ (instancetype) groupPurchaseCellWithGroupPurchase:(GroupPurchase *) groupPurchase {3GroupPurchaseCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"GroupPurchaseCell"owner:nil options:nil] lastObject];45//加载model中的数据,初始化界面6cell.groupPurchase =groupPurchase;78returncell;9}1011/** 没有model数据的空cell*/12+(instancetype)groupPurchaseCell {13return[self groupPurchaseCellWithGroupPurchase:nil];14}

(7)传入model数据的同时,加载数据到view上面

1/** 加载Model数据,初始化界面*/2- (void) setGroupPurchase:(GroupPurchase *) groupPurchase {3if(nil !=groupPurchase) {4self.titleLabel.text =groupPurchase.title;5self.iconImageView.image =[UIImage imageNamed:groupPurchase.icon];6self.priceLabel.text = [NSString stringWithFormat:@"¥%@", groupPurchase.price];7self.buyCountLabel.text = [NSString stringWithFormat:@"%@人已经购买", groupPurchase.buyCount];8}910_groupPurchase =groupPurchase;11}

7.头部广告

其实就是之前的滚动广告放到 self.tableView.tableHeaderView

(1)设计界面

(2)创建class加载图片数据

(3)传入图片名的数组作为成员

1//广告组2@property(nonatomic, strong) NSArray *ads;

(4)加载图片

1/** 设置ads*/2- (void) setAds:(NSArray *)ads {3if(nil !=ads) {4CGFloat adImageWidth =AD_VIEW_WIDTH;5CGFloat adImageHeight =AD_VIEW_HEIGHT;6CGFloat adImageY =0;78for(inti=0; i<ads.count; i++) {9//计算当前图片的水平坐标10CGFloat adImageX = i *adImageWidth;1112UIImageView *adImageView =[[UIImageView alloc] initWithFrame:CGRectMake(adImageX, adImageY, adImageWidth, adImageHeight)];13adImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", ads[i]]];1415[self.scrollView addSubview:adImageView];16}1718//设置滚动19self.scrollView.contentSize = CGSizeMake(ads.count * AD_VIEW_WIDTH,0);20self.scrollView.scrollEnabled =YES;21}2223_ads =ads;24}

(5)在主controller,设置好图片数据,创建头部控件加到头部位置

1//设置头部广告2HeaderAdView *adView = [self genAdView];//手动拼装广告图片数据3self.tableView.tableHeaderView = adView;

(6)添加页码和自动轮播器

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航