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;

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM