您的位置:首页 > 移动开发 > IOS开发

团购(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  团购 IOS开发