本帖最后由 竹林风 于 2018-12-6 11:54 编辑
文章导航
数据源的创建和上节中的一样,使用同一个数据文件。
1.新建一个继承自UITableViewCell的类 FruitNew
代码实现
FruitNew.h
[Objective-C] 纯文本查看 复制代码 #import <UIKit/UIKit.h>
#import "FruitModel.h"
@interface FruitNewCell : UITableViewCell
@property (nonatomic, strong) UIImageView *imgIcon;
@property (nonatomic, strong) UILabel *lblName;
@property (nonatomic, strong) UILabel *lblDesc;
@property (nonatomic, strong) FruitModel *fruitModel;
+ (CGFloat)getCellHeightByDesc:(NSString *)strDesc;
//封装一个创建自定义cell的方法
+ (instancetype)fruitCellWithTableView:(UITableView *)tableView;
@end
FruitNew.m
[Objective-C] 纯文本查看 复制代码 #import "FruitNewCell.h"
@implementation FruitNewCell
+ (CGFloat)getCellHeightByDesc:(NSString *)strDesc{
return 5 + 20 + [strDesc boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
}
- (instancetype) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle = UITableViewCellSelectionStyleNone;
if (!_imgIcon) {
_imgIcon = [UIImageView new];
_imgIcon.frame = CGRectMake(8, 5, 40, 40);
[self.contentView addSubview:_imgIcon];
}
if (!_lblName) {
_lblName = [UILabel new];
_lblName.frame = CGRectMake(CGRectGetMaxX(_imgIcon.frame), CGRectGetMinX(_imgIcon.frame), 150, 20);
_lblName.font = [UIFont systemFontOfSize:14];
[self.contentView addSubview:_lblName];
}
if (!_lblDesc) {
_lblDesc = [UILabel new];
_lblDesc.frame = CGRectMake(CGRectGetMinX(_lblName.frame), CGRectGetMaxY(_lblName.frame), CGRectGetWidth(_lblName.frame), CGRectGetHeight(_lblName.frame));
_lblDesc.font = [UIFont systemFontOfSize:13];
_lblDesc.numberOfLines = 0;
_lblDesc.textColor = [UIColor lightGrayColor];
[self.contentView addSubview:_lblDesc];
}
}
return self;
}
//重写set方法
-(void)setFruitModel:(FruitModel *)fruitModel{
_fruitModel = fruitModel;
self.lblName.text = [NSString stringWithFormat:@"%@(%@)",fruitModel.name,fruitModel.egname];
self.imgIcon.image = [UIImage imageNamed: fruitModel.icon];
self.lblDesc.text = fruitModel.desc;
CGFloat descHeight = [fruitModel.desc boundingRectWithSize:CGSizeMake(150, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil].size.height;
CGRect frame = self.lblDesc.frame;
frame.size.height = descHeight;
self.lblDesc.frame = frame;
}
+ (instancetype)fruitCellWithTableView:(UITableView *)tableView{
static NSString *ID = @"fruit_cell";
FruitNewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[FruitNewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];//不能加xib后缀
}
return cell;
}
数据源协议实现
firstVC.m
[Objective-C] 纯文本查看 复制代码 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
FruitModel *model = self.mAryFruit[indexPath.row];
return [FruitNewCell getCellHeightByDesc:model.desc];
}
/**
* 告诉tableView第section组有多少行
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.mAryFruit.count;
}
/**
* 告诉tableView第indexPath行显示怎样的cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
/*//1.获取模型数据
FruitModel *model = self.mAryFruit[indexPath.row];
//2.创建单元格
//通过xib创建单元格
//由于此方法调用十分频繁,cell的标示声明成静态变量有利于性能优化
static NSString *ID = @"fruit_cell"; //要在xib中设置这个id
FruitCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
//加载xib文件,loadNibName:方法返回的是一个数组,因为xib中可能有不止一个控件
cell = [[[NSBundle mainBundle]loadNibNamed:@"FruitCell" owner:nil options:nil] firstObject];//不能加xib后缀
}
//3.把模型数据设置给单元格
cell.lblName.text = model.name;
cell.lblEgname.text = model.egname;
cell.imgIcon.image = [UIImage imageNamed: model.icon];
//4.返回单元格
return cell; */
FruitModel *model = self.mAryFruit[indexPath.row];
FruitNewCell *cell = [FruitNewCell fruitCellWithTableView:tableView];
cell.fruitModel = model;
return cell;
}
运行效果:
附件:
Test-纯代码自定义cell.zip
(164.99 KB, 下载次数: 8)
|