团购(cell高度相同时自定义cell)
来源:互联网 发布:域名怎么投资 编辑:程序博客网 时间:2024/06/05 05:11
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
@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
@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
0 0
- 团购(cell高度相同时自定义cell)
- 自定义cell的高度
- UI_UItableView_AutoCell(自定义cell 高度)
- 自定义cell的高度
- xib 自定义cell高度
- 通过代码自定义cell(cell的高度不一致)
- 微博(cell高度不一样的自定义cell)
- 自定义cell(通过代码,cell得高度不一致)
- 纯代码自定义cell(cell高度不一致)
- xib自定义cell,返回cell高度
- cell自定义方式一 高度一致时
- cell自定义方式二 高度不一致时
- iOS 自定义Cell 自适应高度
- iOS 自定义cell的高度
- UI 10 cell 自定义高度
- UI - TableView高级(自定义cell、cell自定义高度)
- 当cell的高度不一致时,如何通过代码自定义cell
- iOS通过代码自定义cell(cell的高度不同)的步骤
- Android Studio 初体验,从Eclipse到AS
- 10934 - Dropping water balloons(DP)
- 3.hibernate基础配置
- 关于eclipse安装Genymotion插件的方法
- [LeetCode][Java] Remove Duplicates from Sorted List
- 团购(cell高度相同时自定义cell)
- 记录一下cocos2d-x——开发环境安装
- Hadoop—MapReduce进行数据查询和实现推简单荐系统---练习7
- CoreAnimation的使用小结
- codevs1041
- php实现单链表
- hdu1159
- Android 四大组件学习之Activity三
- 输出一个字符串的所有自字符串(不重复)