团购(cell高度相同时自定义cell)
2015-07-19 11:01
471 查看
Model:
<span style="font-size:14px;">/** * 标题 */ @property (nonatomic, copy) NSString *title; /** * 价格 */ @property (nonatomic, copy) NSString *price; /** * 图片 */ @property (nonatomic, copy) NSString *icon; /** * 购买人数 */ @property (nonatomic, copy) NSString *buyCount; + (instancetype)tgWithDict:(NSDictionary *)dict; - (instancetype)initWithDict:(NSDictionary *)dict; @end</span>
#import "MJTg.h" @implementation MJTg + (instancetype)tgWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } - (instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } @end
View:
(1)xib定义cell
@class MJTg; @interface MJTgCell : UITableViewCell /** * 通过一个tableView来创建一个cell */ + (instancetype)cellWithTableView:(UITableView *)tableView; /** * 团购模型 */ @property (nonatomic, strong) MJTg *tg; @end @interface MJTgCell() //这些属性均从xib中做了连线 @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UILabel *titleView; @property (weak, nonatomic) IBOutlet UILabel *priceView; @property (weak, nonatomic) IBOutlet UILabel *buyCountView; @end @implementation MJTgCell + (instancetype)cellWithTableView:(UITableView *)tableView { static NSString *ID = @"tg"; MJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; if (cell == nil) { // 从xib中加载cell cell = [[[NSBundle mainBundle] loadNibNamed:@"MJTgCell" owner:nil options:nil] lastObject]; } return cell; } - (void)setTg:(MJTg *)tg { _tg = tg; // 1.图片 self.iconView.image = [UIImage imageNamed:tg.icon]; // 2.标题 self.titleView.text = tg.title; // 3.价格 self.priceView.text = [NSString stringWithFormat:@"¥%@", tg.price]; // 4.购买数 self.buyCountView.text = [NSString stringWithFormat:@"%@人已购买", tg.buyCount]; } @end
(2)xib定义尾视图
@class MJTgFooterView; /** 1.协议名称: 控件类名 + Delegate 2.代理方法普遍都是@optional 3. */ @protocol MJTgFooterViewDelegate <NSObject> @optional - (void)tgFooterViewDidClickedLoadBtn:(MJTgFooterView *)tgFooterView; @end @interface MJTgFooterView : UIView /** * 快速创建一个footerView对象 */ + (instancetype)footerView; @property (nonatomic, weak) id<MJTgFooterViewDelegate> delegate; @end @interface MJTgFooterView() - (IBAction)loadBtnClick; @property (weak, nonatomic) IBOutlet UIButton *loadBtn; @property (weak, nonatomic) IBOutlet UIView *loadingView; @end @implementation MJTgFooterView + (instancetype)footerView { return [[[NSBundle mainBundle] loadNibNamed:@"MJTgFooterView" owner:nil options:nil] lastObject]; } /** * 点击"加载"按钮 */ - (IBAction)loadBtnClick { // 1.隐藏加载按钮 self.loadBtn.hidden = YES; // 2.显示"正在加载" self.loadingView.hidden = NO; // 3.显示更多的数据 // GCD dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 3.0s后执行block里面的代码 // 通知代理 if ([self.delegate respondsToSelector:@selector(tgFooterViewDidClickedLoadBtn:)]) { [self.delegate tgFooterViewDidClickedLoadBtn:self]; } // 4.显示加载按钮 self.loadBtn.hidden = NO; // 5.隐藏"正在加载" self.loadingView.hidden = YES; }); } @end
(3)xib定义头视图 这是个工具类 返回一个头视图
@interface MJTgHeaderView : UIView + (instancetype)headerView; @end @interface MJTgHeaderView() @property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation MJTgHeaderView + (instancetype)headerView { return [[[NSBundle mainBundle] loadNibNamed:@"MJTgHeaderView" owner:nil options:nil] lastObject]; // headerView.scrollView } /** * 通过代码创建初始化一个对象的时候,才会调用这个方法 */ //- init //{ // //} /** * 当一个对象从xib中创建初始化完毕的时候就会调用一次 */ - (void)awakeFromNib { // 在这里面添加图片轮播器 } //- (id)initWithCoder:(NSCoder *)aDecoder //{ // //} @end
Control
@interface MJViewController () <UITableViewDataSource, MJTgFooterViewDelegate> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic, strong) NSMutableArray *tgs; @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置每一行cell的高度 self.tableView.rowHeight = 80; // 设置footerView MJTgFooterView *footer = [MJTgFooterView footerView]; footer.delegate = self; self.tableView.tableFooterView = footer; // 设置headerView self.tableView.tableHeaderView = [MJTgHeaderView headerView]; } #pragma mark - MJTgFooterViewDelegate方法 /** * 加载更多的数据 */ - (void)tgFooterViewDidClickedLoadBtn:(MJTgFooterView *)tgFooterView { #warning 正常开发:发送网络请求给远程的服务器 // 1.添加更多的模型数据 MJTg *tg = [[MJTg alloc] init]; tg.icon = @"ad_01"; tg.title = @"新增加的团购数据.."; tg.price = @"100"; tg.buyCount = @"0"; [self.tgs addObject:tg]; // 2.刷新表格(告诉tableView重新加载模型数据, 调用tableView的reloadData) [self.tableView reloadData]; } /** * 隐藏状态栏 */ - (BOOL)prefersStatusBarHidden { return YES; } /** * 数据的懒加载 */ - (NSMutableArray *)tgs { if (_tgs == nil) { // 初始化 // 1.获得plist的全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]; // 2.加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 NSMutableArray *tgArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1.创建模型对象 MJTg *tg = [MJTg tgWithDict:dict]; // 3.2.添加模型对象到数组中 [tgArray addObject:tg]; } // 4.赋值 _tgs = tgArray; } return _tgs; } #pragma mark - 数据源方法 /** * 一共有多少行数据 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.tgs.count; } /** * 每一行显示怎样的cell */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 1.创建cell MJTgCell *cell = [MJTgCell cellWithTableView:tableView]; // 2.给cell传递模型数据 cell.tg = self.tgs[indexPath.row]; return cell; } @end
源码:链接: http://pan.baidu.com/s/1c0pq5m4 密码: y941
相关文章推荐
- 团购、定时抢购倒计时js版
- iOS开发之路--微博“更多”页面
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios开发之环境搭建-01
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- 高鹏能否满座?
- 学习Objective-C:入门手册
- iOS开发 简单实现视频音频的边下边播
- 如何应对团购鼻祖Groupon的“***”
- iOS开发 — Quartz 2D知识点应用 (制作了一个Demo,源代码)
- ios开发对配置文件plist的操作
- iphone5和4的屏幕兼容问题
- 9秒的课程只为培养靠谱的IOS和HTML5开发人才而定制