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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 3154|回复: 4

[新手开发之旅] iOS新手开发之旅-iOS通过xib自定义tableViewCell

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

文章导航  

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


创建本地plist文件数据:

2C422FA9-5C4A-477C-81F0-7EA25C3AC81F.png     DE756AC67A615153D6FFFAC5BE38D928.jpg

创建完成结构如下:

007242A7B8EECE21FAF94B0B938ABEA7.jpg

创建数据模型

916F80DEF2A604C2A93A5F749572181D.png     86DAD0A5D1643001BF7624D2DBBAF641.png

FruitModel.h

[Objective-C] 纯文本查看 复制代码
#import <Foundation/Foundation.h>

@interface FruitModel : NSObject

@property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSString *egname;

@property (nonatomic, strong) NSString *icon;

@property (nonatomic, strong) NSString *desc;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)fruitWithDict:(NSDictionary *)dict;

@end


FruitModel.m

[Objective-C] 纯文本查看 复制代码
#import "FruitModel.h"

@implementation FruitModel

-(instancetype)initWithDict:(NSDictionary *)dict{
    
    if (self == [super init]) {
        self.name = dict[@"name"];
        self.egname = dict[@"egname"];
        self.icon = dict[@"icon"];
        self.desc = dict[@"desc"];
    }
    return self;
}

+ (instancetype)fruitWithDict:(NSDictionary *)dict{
    
    return [[self alloc] initWithDict:dict];
}

@end


读取本地数据文件并加载数据

[Objective-C] 纯文本查看 复制代码
#import "firstVC.h"
#import "FruitModel.h"


@interface firstVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *mAryFruit;

@end

@implementation firstVC

-(NSMutableArray *)mAryFruit{
    if (_mAryFruit == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Fruit.plist" ofType:nil];
        NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
        //字典转模型
        NSMutableArray *arrayModels = [NSMutableArray array];
        for (NSDictionary *dict in arrayDict) {
            FruitModel *model = [FruitModel fruitWithDict:dict];
            [arrayModels addObject:model];
        }
        _mAryFruit = arrayModels;
    }
    return _mAryFruit;
}


用xib方式创建一个文件。在xib中拖一个UITableViewCell,设置高宽。向UITableViewCell中拖子控件。


C9717210479DCBB8942DC2CF96104509.png    D89DB6653D858EC3629A13B6E6602304.png

创建一个继承自UITableViewCell的类ProductCell与xib文件的cell相关联。通过拖线的方式将cell的子控件拖线到ProductCell的属性上。


7DF38955131D4500180E436DC68753C8.png

实现数据源协议:firstVC.m

[Objective-C] 纯文本查看 复制代码
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

/**
 *  告诉tableView第section组有多少行
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _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;
}


这里有个问题,如果在控制器中直接为cell中的每个子控件赋值数据可能会造成以下问题


1.控制器强依赖于cell,一旦cell内部子控件发生了变化,那么控制器中的代码也得改(紧耦合)。控制器完全依赖于单元格里面的属性。
2.cell的封装不够完整,凡是用到cell的地方,每次都要编写为cell的子控件依次赋值的语句,比如:cell.xxx = model.xxx。如果有10个控制器,每个控制器里都需要用到单元格进行赋值,如果一个单元格里有10个子控件,那么上面这样的代码就要写10次。


所以我们可以这样:

对自定义cell进行封装,把模型数据设置给单元格,形如:cell.goods = model;由cell对象内部自己来解析模型数据,并把数据设置到对应的子控件中。在cell中创建一个模型类型的属性,重写该属性的set方法,在set方法中将数据赋值给控件。


修改后的代码:


FruitCell.h

[Objective-C] 纯文本查看 复制代码
#import <UIKit/UIKit.h>
#import "FruitModel.h"

@interface FruitCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imgIcon;
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) IBOutlet UILabel *lblEgname;

@property (nonatomic, strong) FruitModel *fruitModel;

//封装一个创建自定义cell的方法
+ (instancetype)FruitCellWithTableView:(UITableView *)tableView;


FruitCell.m

[Objective-C] 纯文本查看 复制代码
#import "FruitCell.h"

@implementation FruitCell


//重写set方法
-(void)setFruitModel:(FruitModel *)fruitModel{
    
    _fruitModel = fruitModel;
    
    self.lblName.text = fruitModel.name;
    self.lblEgname.text = fruitModel.egname;
    self.imgIcon.image = [UIImage imageNamed: fruitModel.icon];
}

+ (instancetype)FruitCellWithTableView:(UITableView *)tableView{
    static NSString *ID = @"fruit_cell";
    FruitCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"FruitCell" owner:nil options:nil] firstObject];//不能加xib后缀
    }
    return cell;
}

@end


修改后的数据源方法

8EF75A2591082B285198381EFD05C87C.png

运行效果
62D68C95CFBFC52B0E048DFFB8B33A3C.jpg

附件: Test-xib自定义cell.zip (164.89 KB, 下载次数: 3)


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-5 18:00:22 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-5 18:01:02 | 显示全部楼层
这些我都爱吃
发表于 2018-12-5 18:01:24 | 显示全部楼层
很详细的帖子,谢谢楼主分享
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-24 20:33

© 2014-2021

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