cell自定义方式一 高度一致时

来源:互联网 发布:图像识别算法matlab 编辑:程序博客网 时间:2024/06/08 05:42


//——-----------------当cell的高度一致时使用XIB--------------------


步骤:

1.>新建一个xib文件描述一个view的内部结构(假设叫做MJTgCell.xib) 
 
2.>新建一个自定义的类
  (自定义类需要继承自系统自带的view,继承自哪个类,  取决于xib根对象的Class)
 
3.>新建类的类名最好跟xib的文件名保持一致(比如类名就叫做MJTgCell) 
 
4.>xib中的控件  自定义类的.m文件 进行连线 
 
5.>提供一个类方法返回一个创建好的自定义view(屏蔽从xib加载的过程) 
 
6.>提供一个模型属性让外界传递模型数据 
 
7.>重写模型属性的setter方法,在这里将模型数据展示到对应的子控件上面


//——---------------------———————————模型文件M----------------------------------------
继承于NSObject.h#import <Foundation/Foundation.h>@interface MJTg : NSObject/** *  标题 */@property (nonatomic,copy)NSString *title;/** *  价格 */@property (nonatomic,copy)NSString *price;/** *  图片 */@property (nonatomic,copy)NSString *icon;/** *  购买人数 */@property (nonatomic,copy)NSString *buyCount;+ (instancetype)tgWithDict:(NSDictionary *)dict;- (instancetype)initWithDict:(NSDictionary *)dict;@end.m#import "MJTg.h"@implementation MJTg+ (instancetype)tgWithDict:(NSDictionary *)dict{    return [[selfalloc]initWithDict:dict];}- (instancetype)initWithDict:(NSDictionary *)dict{    if (self = [superinit]) {        [self setValuesForKeysWithDictionary:dict];    }    return self;}@end



//——---------------------———————————视图文件V----------------------------------------

继承于UITableViewCell.h#import <UIKit/UIKit.h>@class MJTg;@interface MJTgCell : UITableViewCell/** *  通过一个tableView来创建一个cell */+ (instancetype)cellWithTableView:(UITableView *)tableView;/** *  团购模型 */@property (nonatomic,strong)MJTg *tg;@end

 


XIB文件,通过脱线获得属性,红圈处可以设置cell的ID标示



m#import "MJTgCell.h"#import "MJTg.h"@interface MJTgCell()//这些属性均从xib中做了连线@property (weak,nonatomic)IBOutlet UIImageView *iconView;@property (weak,nonatomic)IBOutlet UILabel *titleView;@property (weak,nonatomic)IBOutlet UILabel *priceView;@property (weak,nonatomic)IBOutlet UILabel *buyCountView;@end@implementation MJTgCell+ (instancetype)cellWithTableView:(UITableView *)tableView{    static NSString *ID = @"tg";    MJTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    if (cell == nil) {        // 从xib中加载cell        cell = [[[NSBundle mainBundle] loadNibNamed:@"MJTgCell"owner:niloptions:nil]lastObject];    }    return cell;}- (void)setTg:(MJTg *)tg{    _tg = tg;       // 1.图片    self.iconView.image = [UIImageimageNamed:tg.icon];       // 2.标题    self.titleView.text = tg.title;       // 3.价格    self.priceView.text = [NSStringstringWithFormat:@"¥%@", tg.price];       // 4.购买数    self.buyCountView.text = [NSStringstringWithFormat:@"%@人已购买", tg.buyCount];}@end



//——---------------------———————视图控制器文件C----------------------------------------

继承于UIViewController@property (nonatomic,strong)NSMutableArray *tgs;/** *  数据的懒加载 */- (NSMutableArray *)tgs{    if (_tgs ==nil) {        // 初始化        // 1.获得plist的全路径        NSString *path = [[NSBundlemainBundle]pathForResource:@"tgs.plist"ofType:nil];               // 2.加载数组        NSArray *dictArray = [NSArrayarrayWithContentsOfFile:path];               // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中        NSMutableArray *tgArray = [NSMutableArrayarray];        for (NSDictionary *dictin dictArray) {            // 3.1.创建模型对象            MJTg *tg = [MJTgtgWithDict:dict];                       // 3.2.添加模型对象到数组中            [tgArray addObject:tg];        }               // 4.赋值        _tgs = tgArray;    }    return _tgs;}#pragma mark - 数据源方法/** *  一共有多少行数据 */- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return self.tgs.count;}/** *  每一行显示怎样的cell */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1.创建cell    MJTgCell *cell = [MJTgCellcellWithTableView:tableView];       // 2.给cell传递模型数据    cell.tg = self.tgs[indexPath.row];    return cell;}

 
0 0
原创粉丝点击