iOS开发----使用纯代码自定义UItableviewcell实现一个简单的界面布局

使用纯代码自定义一个tableview的步骤:

1.新建一个继承自UITableViewCell的类

2.重写initWithStyle:reuseIdentifier:方法

添加所有需要显示的子控件(子控件要添加到contentView中)

3.数据模型: 存放文字数据\图片数据

4.为模型赋值,并通过模型给cell赋值

 

代码实现

 

GKTableviewcell.h

#import <UIKit/UIKit.h>

#import "Model.h"

 

@interface GKTableViewCell : UITableViewCell

 

@property (nonatomic, strong) Model *showModel;

 

 + (instancetype)cellWithTableView:(UITableView *)tableView;

 

@end

 

GKTableviewcell.m

 

#import "GKTableViewCell.h"

  @interface GKTableViewCell ()

//头像

 @property (nonatomic, weak) UIImageView *headImageView;

//昵称

 @property (nonatomic, weak) UILabel *nameLabel;

 

 @end

 

@implementation GKTableViewCell{

    

}

 

- (void)awakeFromNib {

    [super awakeFromNib]; 

    // Initialization code

}

 

+ (instancetype)cellWithTableView:(UITableView *)tableView

{

         static NSString *identifier = @"status";

         // 1.缓存中取

         GKTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

         // 2.创建

         if (cell == nil) {

                 cell = [[GKTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];

             }

         return cell;

}

 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

 {

         self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

         if (self) {

                 // 让自定义Cell和系统的cell一样, 一创建出来就拥有一些子控件提供给我们使用

                 // 1.创建头像

                 UIImageView *headImageView = [[UIImageView alloc] init];

                 [self.contentView addSubview:headImageView];

                 self.headImageView = headImageView;

        

                 // 2.创建昵称

                 UILabel *nameLabel = [[UILabel alloc] init];

             nameLabel.textColor = [UIColor whiteColor];

             nameLabel.font = [UIFont systemFontOfSize:25];

                 [self.contentView addSubview:nameLabel];

                 self.nameLabel = nameLabel;

        

             }

         return self;

     }

 

- (void)setShowModel:(MessageModel *)showModel{

    _showModel = showModel;

//赋值

    [_headImageView sd_setImageWithURL:[NSURL URLWithString:showModel.headImage]];

    _nameLabel.text = showModel.name;

    

    [self settingFrame];

}

 

//设置子控件的frame

 - (void)settingFrame

 {

    

            // 设置头像的frame

      self.headImageView.sd_layout

     .widthIs(50)

     .heightIs(50)

     .topSpaceToView(self.contentView, 12)

     .leftSpaceToView(self.contentView, 12)

     .bottomSpaceToView(self.contentView, 10);

     self.headImageView.layer.masksToBounds = YES;

     self.headImageView.layer.cornerRadius = 25;

    

         // 设置昵称的frame

             self.nameLabel.sd_layout

     .heightIs(15)

     .topSpaceToView(self.contentView, 12)

     .leftSpaceToView(self.contentView, 70)

     .rightSpaceToView(self.contentView, 90);

  

     }

 

 - (CGSize)sizeWithString:(NSString *)str font:(UIFont *)font maxSize:(CGSize)maxSize

 {

         NSDictionary *dict = @{NSFontAttributeName : font};

         // 如果将来计算的文字的范围超出了指定的范围,返回的就是指定的范围

         // 如果将来计算的文字的范围小于指定的范围, 返回的就是真实的范围

         CGSize size =  [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;

         return size;

}

Model.h

#import <Foundation/Foundation.h>

 

@interface Model : NSObject

 

//头像

@property (nonatomic, weak) UIImageView *headImageView;

//昵称

@property (nonatomic, weak) UILabel *nameLabel;

 

- (instancetype)initWithDictionary:(NSDictionary *)dictionary;

 

@end

 

Model.m

#import "Model.h"

 

@implementation Model

 

-(instancetype)initWithDictionary:(NSDictionary *)dictionary{

    self = [super init];

    if (self) {

        [self setValuesForKeysWithDictionary:dictionary];

    }

    return self;

}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{

    NSLog(@"%@",key);

}

 

@end

 

ViewController.m

#import "ViewController.h"

#import "GKTableViewCell.h"

#impot "Model.h"

 

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{

    

}

 

@property (nonatomic, strong) NSMutableArray *dataSource;

 

@end

 

@implementation ViewController

 

#pragma mark - 懒加载

 - (NSArray *)dataSource

 {

         if (_dataSource == nil) {

                 NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"statuses.plist" ofType:nil];

                 NSArray *dictArray = [NSArray arrayWithContentsOfFile:fullPath];

                 NSMutableArray *Marray = [NSMutableArray arrayWithCapacity:dictArray.count];

                 for (NSDictionary *dict in dictArray) {

                         // 创建模型

                         Model *model = [Model initWithDictionary:dict];

                         [Marray addObject:model];

                     }

                 self.dataSource = [Marray copy];

             }

         return dataSource;

     }

 

 

#pragma mark - UITableViewDataSource

 

//分区行数

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

 

            return self.dataSource.count;

 

}

//展示数据

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 

    GKTableViewCell *cell = [GKTableViewCell cellWithTableView:tableView];

            cell.showModel = self.dataSource[indexPath.row];

 

    return cell;

}

 

posted @ 2017-02-11 16:56  浮生こ若梦  阅读(6824)  评论(0编辑  收藏  举报