您的位置:首页 > 其它

自定义等高的cell(storyboard)

2016-02-04 22:09 183 查看
/*
storyboard自定义cell
1.创建一个继承自UITabelViewCell的子类,比如tgCell
2.在storyboard中:
往cell里面增加需要用到的子控件;
设置cell的重用标识;
设置cell的class为tgCell
3.在控制器中:
利用重用标识找到cell;
给cell传递模型数据
4.在tgCell中:
将storyboard中的子控件连线到类扩展中;
需要提供一个模型属性,重写该属性的set方法,在这个方法中设置模型数据到子控件上
*/








//
//  TableViewController.m
#import "TableViewController.h"
#import "tgModel.h"
#import "tgCell.h"

@interface TableViewController ()
/** 所有团购数据 */
@property(nonatomic,strong) NSArray *tgs;
@end

@implementation TableViewController

- (NSArray *)tgs
{
if (_tgs == nil) {
// 加载plist文件中的字典数组
NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];

// 字典数组 -> 模型数组
NSMutableArray *arrayM = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
tgModel *model = [tgModel tgWithDict:dict];
[arrayM addObject:model];
}

_tgs = arrayM;
}
return _tgs;
}

- (void)viewDidLoad {
[super viewDidLoad];

}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return self.tgs.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *ID = @"cell";
tgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

//传递模型数据
cell.model = self.tgs[indexPath.row];

return cell;
}

@end
模型:
//
//  tgModel.h
//  模型

#import <Foundation/Foundation.h>

@interface tgModel : NSObject
//注意这里用的是 strong
@property(nonatomic,strong)NSString *buyCount;
@property(nonatomic,strong)NSString *price;
@property(nonatomic,strong)NSString *title;
@property(nonatomic,strong)NSString *icon;

/**
*  字典转模型
*/
+ (instancetype)tgWithDict:(NSDictionary *)dict;
@end
#import "tgModel.h"

@implementation tgModel
+ (instancetype)tgWithDict:(NSDictionary *)dict
{
tgModel *model = [[self alloc] init];

//    model.title = dict[@"title"];
//    model.price = dict[@"price"];
//    model.buyCount = dict[@"buyCount"];
//    model.icon = dict[@"icon"];

//KVC - key value coding
[model setValuesForKeysWithDictionary:dict];

return model;
}
@end
自定义的cell:

//
//  tgCell.h

#import <UIKit/UIKit.h>

@class tgModel;

@interface tgCell : UITableViewCell
/**
*  团购模型数据
*/
@property(nonatomic,strong)tgModel *model;
@end
//
//  tgCell.m

#import "tgCell.h"
#import "tgModel.h"

@interface tgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buycountLabel;

@end

@implementation tgCell

- (void)setModel:(tgModel *)model
{
_model = model;

//设置数据
self.iconView.image = [UIImage imageNamed:model.icon];
self.titleLabel.text = model.title;
self.priceLabel.text = [NSString stringWithFormat:@"¥%@",model.price];
self.buycountLabel.text = [NSString stringWithFormat:@"%@人已购买",model.buyCount];
}
@end
用到的tgs.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>buyCount</key>
<string>32</string>
<key>icon</key>
<string>37e4761e6ecf56a2d78685df7157f097.png</string>
<key>price</key>
<string>98</string>
<key>title</key>
<string>味妙烤鱼吧</string>
</dict>
<dict>
<key>buyCount</key>
<string>3</string>
<key>icon</key>
<string>5ee372ff039073317a49af5442748071.png</string>
<key>price</key>
<string>128</string>
<key>title</key>
<string>桃园酒家</string>
</dict>
<dict>
<key>buyCount</key>
<string>1</string>
<key>icon</key>
<string>2c97690e72365e38e3e2a95b934b8dd2.png</string>
<key>price</key>
<string>88</string>
<key>title</key>
<string>俏巴蜀</string>
</dict>
<dict>
<key>buyCount</key>
<string>11</string>
<key>icon</key>
<string>9b437cdfb3e3b542b5917ce2e9a74890.png</string>
<key>price</key>
<string>39</string>
<key>title</key>
<string>蜜语芭蕾休闲西餐厅</string>
</dict>
<dict>
<key>buyCount</key>
<string>3</string>
<key>icon</key>
<string>a5f46774487027f73ddb35dbb6e5a41c.png</string>
<key>price</key>
<string>108</string>
<key>title</key>
<string>Grinders 西餐酒吧</string>
</dict>
<dict>
<key>buyCount</key>
<string>3</string>
<key>icon</key>
<string>2010e3a0c7f88c3f5f5803bf66addd93.png</string>
<key>price</key>
<string>58</string>
<key>title</key>
<string>昆达理尼瑜伽馆黄金殿</string>
</dict>
<dict>
<key>buyCount</key>
<string>1</string>
<key>icon</key>
<string>eb83ba2a2bf3098e324350ce88d6b862.png</string>
<key>price</key>
<string>999</string>
<key>title</key>
<string>正院大宅门</string>
</dict>
<dict>
<key>buyCount</key>
<string>2</string>
<key>icon</key>
<string>d40878ee9d97a53bd4b8778daa11d38d.png</string>
<key>price</key>
<string>78</string>
<key>title</key>
<string>江边码头烤鱼</string>
</dict>
<dict>
<key>buyCount</key>
<string>1094</string>
<key>icon</key>
<string>97726b804a2558308098bd59edc833fa.png</string>
<key>price</key>
<string>85.5</string>
<key>title</key>
<string>将太无二</string>
</dict>
<dict>
<key>buyCount</key>
<string>1</string>
<key>icon</key>
<string>7003217f16ed29bab85e635a3bd6b60d.png</string>
<key>price</key>
<string>128</string>
<key>title</key>
<string>巴蜀轩烤鱼专家</string>
</dict>
<dict>
<key>buyCount</key>
<string>2</string>
<key>icon</key>
<string>a6c484045581f45e50db68e4f5dc734c.png</string>
<key>price</key>
<string>318</string>
<key>title</key>
<string>金色阳光俱乐部</string>
</dict>
<dict>
<key>buyCount</key>
<string>150</string>
<key>icon</key>
<string>3714230523e78c839947694f3e97f0a5.png</string>
<key>price</key>
<string>79</string>
<key>title</key>
<string>川军本色/龙人居酒楼</string>
</dict>
<dict>
<key>buyCount</key>
<string>2</string>
<key>icon</key>
<string>53453be0d2dd458c057286d17f6b9306.png</string>
<key>price</key>
<string>228</string>
<key>title</key>
<string>V SPORTS运动餐吧</string>
</dict>
</array>
</plist>
最后效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: