UITableView_XIB实现_可重用Cell_封装步骤

本文详细介绍了如何在应用开发中利用MVCD分组进行工程组织,创建模型(如MainModel),并结合XIB文件创建自定义视图(如MainViewCell)。通过实例解析了模型类(MainModel)的属性和接口实现,以及视图类(MainViewCell)如何与模型交互,展示了如何通过绑定属性和接口来实现场景中的数据展示。
1.创建工程,根据功能MVCD分组
2.创建模型,例如(MainModel)——————————例如有一个图片和标题
<1>MainModel.h文件实现属性和接口
<2>MainModel.m文件方法实现
3.创建XIB和对应视图,例如(MainViewCell)
<1>XIB绑定MainViewCell类,设置可回收属性
<2>MainViewCell.h文件
(1)导入@class MianBean;在#import <UIKit/UIKit.h>后面加入
(2)设置模型成属性@property (nonatomic ,strong) MianBean *mianBean;在@interface和@end之间加入
(3)设置接口+ (instancetype)cellWithTableView:(UITableView *)tableView;在@interface和@end之间加入
<3>MainViewCell.m文件
(1)导入#import “MianBean.h”在#import "MainViewCell.h"后面加入
(2)在导入文件后增加,根据模型的属性设置,并且跟XIB里面的控件实现连线绑定
在@implementation MainViewCell前面加入
@interface  MainViewCell() 
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
                       @property (weak, nonatomic) IBOutlet UILabel *titleLable;
@end
(3)在@implementation MainViewCell后实现MainViewCell.h的接口
+ (instancetype)cellWithTableView:(UITableView *)tableView{  
       static NSString *ID = @"Cell";  // 1. 可重用标示符
                       MainViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 2. tableView查询可重用Cell
   if (cell == nil) {// 3. 如果没有可重用cell
NSLog(@"加载XIB");
cell = [[[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil] lastObject];// 从XIB加载自定义视图
    }
  return cell;
 }
(4)

-(void)setMainModel:(MainModel *)mainModel{

     // setter方法中,第一句要赋值,否则要在其他方法中使用模型,将无法访问到

     _mainModel = mainModel;

     self.iconView.image=[UIImage imageNamed:mainModel.icon];

     self.titleLable.text=mainModel.title;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值