http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 6414|回复: 9

[新手开发之旅] iOS新手开发之旅-纯代码自定义tableViewCell、自适应高度

[复制链接]
发表于 2018-12-5 18:13:40 | 显示全部楼层 |阅读模式
本帖最后由 竹林风 于 2018-12-6 11:54 编辑


文章导航  

【iOS新手开发之旅】   http://www.sufeinet.com/thread-24000-1-1.html


数据源的创建和上节中的一样,使用同一个数据文件。

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;
}


运行效果:

22D295618713EEC67AAD269E1D65BBC0.jpg

附件: Test-纯代码自定义cell.zip (164.99 KB, 下载次数: 8)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-5 18:16:07 | 显示全部楼层
每天跟着你的脚步 学到好多东西了
发表于 2018-12-5 18:16:12 | 显示全部楼层
感恩无私的分享与奉献
 楼主| 发表于 2018-12-5 18:20:01 | 显示全部楼层
Amy 发表于 2018-12-5 18:16
感恩无私的分享与奉献

欢迎提问题
 楼主| 发表于 2018-12-5 18:21:07 | 显示全部楼层
欢迎加入苏飞论坛(IOS),群聊号码:284526528
发表于 2018-12-5 18:22:05 | 显示全部楼层
楼主真勤奋
发表于 2018-12-5 18:58:12 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-5 19:36:42 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-6 15:58:01 | 显示全部楼层
楼主给力,加油, 看好你哦
发表于 2018-12-6 15:58:09 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-3-29 19:54

© 2014-2021

快速回复 返回顶部 返回列表