UITableView原理



//  Copyright © 2016 mac. All rights reserved.

//

#import "ModelFrame.h"

#import "CustomViewController.h"

#import "MessageTableViewCell.h"

#import "Model.h"


#define kScreenWidth     [UIScreen mainScreen].bounds.size.width

#define kScreenHeight        [UIScreen mainScreen].bounds.size.height


@interface CustomViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSMutableArray<ModelFrame*>  *modelArr;


@end


@implementation CustomViewController

-(NSMutableArray *)modelArr{

    

    if (!_modelArr) {

        _modelArr=[[NSMutableArray alloc]init];

    }

    return_modelArr;

}


-(void)initWithData{

    

  

    

    Model  *model=[[Modelalloc]initWithDic:@{@"userName":@"云溪",

                                               @"userIcon":@"50",

                                               @"date":@"今天12:00",

                                              @"text":@"Android是一种基于Linux的自由及开放源代码的操作系统,由Google公司以及开放手持设备联盟领导开发。Android系统的应用覆盖了一定互联网的各个的领域,在智能手机领域,Android堪称统治级,占操作系统比重80.4%Android系统目前已经布满我们生活的各个方面,除了手机、平台电脑、电视、电视盒子、游戏机、手表和车载系统以外还有其他很多设备可能运行着 Android系统,比如已经出品的海尔Android系统智能冰箱,OneBoard Pro+智能机械键盘,宝丽来Socialmatic拍立得以及Android系统座机、Android智能烤箱、Daqri安全帽等等,都在产品的应用中选择了不同的Android操作系统。由于Android行业发展横跨领域众多,人才需求也相对广泛,具有十分广阔的发展空间",

                                               @"imagesArray":@[@"ad_00",@"ad_01",@"ad_02",@"ad_03"]

                                               

                                               }];

    ModelFrame  *frame=[[ModelFramealloc]initWithModel:model];

    NSArray  *arr=[[NSArrayalloc]init];


    

    Model  *model1=[[Modelalloc]initWithDic:@{@"userName":@"王尼玛",

                                               @"userIcon":@"1",

                                               @"date":@"今天13:20",

                                               @"text":@"实力派技术前辈严谨负责的育人态度授之以鱼更授之以渔终身的职业引领者。",

                                               @"imagesArray":arr

                                               

                                               }];

    ModelFrame  *frame1=[[ModelFramealloc]initWithModel:model1];

    

    

    

    Model  *model2=[[Modelalloc]initWithDic:@{@"userName":@"",

                                               @"userIcon":@"424324",

                                               @"date":@"今天12:00",

                                               @"text":@"",

                                               @"imagesArray":@[@"ad_00",@"ad_01",@"ad_02"]

                                               

                                               }];

    

    

    

    ModelFrame  *frame2=[[ModelFramealloc]initWithModel:model2];

    

    

    Model  *model3=[[Model alloc]initWithDic:@{@"userName":@"安心",

                                                 @"userIcon":@"7735164184",

                                                 @"date":@"今天00:00",

                                                 @"text":@"全世界已经失眠",

                                                 @"imagesArray":@[@"238221586"]

                                                 

                                                 }];

    

    ModelFrame   *frame3=[[ModelFramealloc]initWithModel:model3];

    

    

    Model  *model4=[[Model alloc]initWithDic:@{@"userName":@"转身&微笑",

                                                 @"userIcon":@"1516159990",

                                                 @"date":@"今天00:00",

                                                 @"text":@"天气",

                                                 @"imagesArray":@[]

                                                 

                                                 }];

    

    ModelFrame   *frame4=[[ModelFramealloc]initWithModel:model4];

    

    

        [self.modelArr addObject:frame];

        [self.modelArraddObject:frame2];

        [self.modelArraddObject:frame1];

    

    [self.modelArr addObject:frame3];

    [self.modelArraddObject:frame4];

    

    







}


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    [selfinitWithData];


    

    UITableView  *tableView=[[UITableViewalloc]initWithFrame:(CGRect){0,0,kScreenWidth,kScreenHeight}style:UITableViewStylePlain];

    tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;

    tableView.contentInset=UIEdgeInsetsMake(0, 0, 0, 0);

    tableView.delegate=self;

    tableView.dataSource=self;

    [self.view addSubview:tableView];

    

}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}


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

    returnself.modelArr.count;

}


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

    

     MessageTableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:@"message"];

    if (!cell) {

        cell=[[MessageTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"message"];

        

    }

    cell.modelFrame=self.modelArr[indexPath.row];

    

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ModelFrame *modelFrame  = (ModelFrame*)self.modelArr[indexPath.row];

    return modelFrame.cellHeight;

}




@end


//

//  MessageTableViewCell.m

//  TableViewController

//

//  Created by mac on 16/5/27.

//  Copyright © 2016 mac. All rights reserved.

//

#import "Model.h"

#import "MessageTableViewCell.h"

@interface MessageTableViewCell()


@property(nonatomic,strong)UIButton  *iconBtn;

@property(nonatomic,strong)UILabel  *userNameLabel;

@property(nonatomic,strong)UILabel  *dateLabel;

@property(nonatomic,strong)UILabel  *textMLabel;




@property(nonatomic,strong)UIImageView  *imageViewO;

@property(nonatomic,strong)UIImageView  *imageViewW;

@property(nonatomic,strong)UIImageView  *imageViewT;

@property(nonatomic,strong)UIImageView  *imageViewF;

@property(nonatomic,strong)UIImageView  *imageViewV;

@property(nonatomic,strong)UIImageView  *imageViewS;

@end


@implementation MessageTableViewCell


- (void)awakeFromNib {

    // Initialization code

}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [supersetSelected:selectedanimated:animated];


    // Configure the view for the selected state

}


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    self=[superinitWithStyle:stylereuseIdentifier:reuseIdentifier];

    if (self) {

        

        self.selectionStyle=UITableViewCellSelectionStyleNone;

        self.iconBtn=[UIButtonbuttonWithType:UIButtonTypeCustom];

        self.iconBtn.clipsToBounds=YES;

        self.iconBtn.layer.masksToBounds=YES;

        self.iconBtn.layer.cornerRadius=22;

        self.iconBtn.backgroundColor=[UIColorwhiteColor];

        [self.contentView addSubview:self.iconBtn];

        

        

        self.userNameLabel=[[UILabelalloc]init];

        self.userNameLabel.font=[UIFontsystemFontOfSize:15];

        self.userNameLabel.textColor=[UIColorblackColor];

        self.userNameLabel.backgroundColor=[UIColorwhiteColor];

        self.userNameLabel.textAlignment=NSTextAlignmentLeft;

        [self.contentViewaddSubview:self.userNameLabel];

        

        

        

        self.dateLabel=[[UILabelalloc]init];

        self.dateLabel.backgroundColor=[UIColorwhiteColor];

        self.dateLabel.textAlignment=NSTextAlignmentLeft;

        self.dateLabel.textColor=[UIColorgrayColor];

        self.dateLabel.font=[UIFontsystemFontOfSize:14];

        [self.contentViewaddSubview:self.dateLabel];

        

        

        self.textMLabel=[[UILabel alloc]init];

        self.textMLabel.frame=CGRectZero;

        self.textMLabel.font=[UIFontsystemFontOfSize:15];

        self.textMLabel.textColor=[UIColorblackColor];

        self.textMLabel.backgroundColor=[UIColorwhiteColor];

        self.textMLabel.textAlignment=NSTextAlignmentLeft;

        self.textMLabel.numberOfLines=0;

        [self.contentViewaddSubview:self.textMLabel];

        

        

        self.imageViewO=[[UIImageViewalloc]init];

        self.imageViewO.tag=200;

        self.imageViewO.frame=CGRectZero;

        self.imageViewO.clipsToBounds=YES;


        self.imageViewO.backgroundColor=[UIColorwhiteColor];

        self.imageViewO.contentMode=UIViewContentModeScaleAspectFill;

        

        self.imageViewO.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewO.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewO];

        

        

        

        

        

        self.imageViewW=[[UIImageViewalloc]init];

        self.imageViewW.tag=201;

        self.imageViewW.clipsToBounds=YES;

        self.imageViewW.frame=CGRectZero;


        self.imageViewW.backgroundColor=[UIColorwhiteColor];

        self.imageViewW.contentMode=UIViewContentModeScaleAspectFill;


        self.imageViewW.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewW.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewW];

        

        

        

        

        self.imageViewT=[[UIImageViewalloc]init];

        self.imageViewT.tag=202;

        self.imageViewT.frame=CGRectZero;

        self.imageViewT.clipsToBounds=YES;



        self.imageViewT.backgroundColor=[UIColorwhiteColor];

        self.imageViewT.contentMode=UIViewContentModeScaleAspectFill;

        

        self.imageViewT.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewT.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewT];

        

        

        

        

        

        self.imageViewF=[[UIImageViewalloc]init];

        self.imageViewF.tag=203;

        self.imageViewF.clipsToBounds=YES;


        self.imageViewF.frame=CGRectZero;

      

        self.imageViewF.backgroundColor=[UIColorwhiteColor];

        self.imageViewF.contentMode=UIViewContentModeScaleAspectFill;

        

        self.imageViewF.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewF.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewF];

        

        

        

        

        self.imageViewV=[[UIImageViewalloc]init];

        self.imageViewV.tag=204;

        self.imageView.clipsToBounds=YES;

        self.imageViewV.layer.masksToBounds=YES;


        self.imageViewV.frame=CGRectZero;


        self.imageViewV.backgroundColor=[UIColorwhiteColor];

        self.imageViewV.contentMode=UIViewContentModeScaleAspectFill;

        

        self.imageViewV.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewV.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewV];

        

        

        

        self.imageViewS=[[UIImageViewalloc]init];

        self.imageViewS.tag=205;

        self.imageViewS.frame=CGRectZero;

        self.imageViewS.clipsToBounds=YES;



        self.imageViewS.backgroundColor=[UIColorwhiteColor];

        self.imageViewS.contentMode=UIViewContentModeScaleAspectFill;

        

        self.imageViewS.layer.borderColor=[UIColorwhiteColor].CGColor;

        self.imageViewS.layer.borderWidth=1.50f;

        

        [self.contentViewaddSubview:self.imageViewS];

        

        

        

        

        

        

        

    }

    returnself;

}

-(void)setModelFrame:(ModelFrame *)modelFrame{

    /**

     清空重用的数据

     */

    _iconBtn.frame=CGRectZero;

    _userNameLabel.frame=CGRectZero;

    _dateLabel.frame=CGRectZero;

    _textMLabel.frame=CGRectZero;

    _imageViewO.frame=CGRectZero;

    _imageViewF.frame=CGRectZero;

    _imageViewS.frame=CGRectZero;

    _imageViewT.frame=CGRectZero;

    _imageViewV.frame=CGRectZero;

    _imageViewW.frame=CGRectZero;


    

    

    _modelFrame=modelFrame;

    [self setSubViewContent];


    [self setSubViewFrame];


}

-(void)setSubViewContent{

    

    

    Model   *model     =self.modelFrame.model;

    

    [self.iconBtnsetImage:[UIImageimageNamed:model.userIcon]forState:UIControlStateNormal];

    

    self.userNameLabel.text=model.userName;

    

    self.textMLabel.text=model.text;

    

    self.dateLabel.text=model.date;

    

    

    [model.imagesArrayenumerateObjectsUsingBlock:^(NSString  *imageName,NSUInteger idx,BOOL *_Nonnull stop) {

        

       UIImageView *imageView =(UIImageView*)[self.contentViewviewWithTag:200+idx];

        imageView.image=[UIImageimageNamed:imageName];

        

        

    }];

    

    

    

    

    

    

    

    

}

-(void)setSubViewFrame{

    self.iconBtn.frame=self.modelFrame.iconFrame;

    self.userNameLabel.frame=self.modelFrame.userNameFrame;

    self.dateLabel.frame=self.modelFrame.dateFrame;


    if (![self.modelFrame.model.textisEqualToString:@""]) {

        self.textMLabel.frame=self.modelFrame.textFrame;


    }

    if (self.modelFrame.model.imagesArray.count>0) {

        

    

    [self.modelFrame.imagesArrayFramesenumerateObjectsUsingBlock:^(NSString *obj,NSUInteger idx, BOOL *_Nonnull stop) {

        UIImageView *imageView =(UIImageView*)[self.contentViewviewWithTag:200+idx];


        imageView.frame=CGRectFromString(obj);

    }];

    }

    

}


@end




//

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

@class Model;

@interface ModelFrame : NSObject


@property(nonatomic,assign)CGRect  userNameFrame;


@property(nonatomic,assign)CGRect  iconFrame;


@property(nonatomic,assign)CGRect  dateFrame;

@property(nonatomic,assign)CGRect  textFrame;

@property(nonatomic,strong)NSArray  *imagesArrayFrames;


//@property(nonatomic,strong)NSArray  *commentArrayFrames;


@property(nonatomic,strong)Model  *model;



@property(nonatomic,assign)CGFloat  cellHeight;



- (instancetype)initWithModel:(Model*)model;










@end


//

#import "Model.h"

#import "ModelFrame.h"



#define kScreenWidth             [UIScreen mainScreen].bounds.size.width



@implementation ModelFrame

- (instancetype)initWithModel:(Model*)model;

{

    self = [superinit];

    if (self) {

        self.model=model;

       __blockCGFloat margin=14;

        

        self.iconFrame=CGRectMake(10, 10, 44, 44);

        self.userNameFrame=CGRectMake(self.iconFrame.origin.x+self.iconFrame.size.width+3, 10, 120, 24);

        self.dateFrame=CGRectMake(self.iconFrame.origin.x+self.iconFrame.size.width+3, 34, 120, 22);

     __block  CGFloat previousHeight=margin+self.iconFrame.size.height;

        if (![self.model.textisEqualToString:@""]) {


        CGRect   textBounds =[self.model.textboundingRectWithSize:CGSizeMake(kScreenWidth-30, 0)options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}context:nil];

            self.textFrame=CGRectMake(10, margin+self.iconFrame.size.height,kScreenWidth-20, textBounds.size.height);

            previousHeight =previousHeight+margin+textBounds.size.height;

            

            

        }

        

        if (self.model.imagesArray.count>0) {

            CGFloat imageHeight ;

            NSInteger imageCount =self.model.imagesArray.count;

            

            if (imageCount==1) {

                imageHeight=(kScreenWidth/3)*2;


            }else if(imageCount==2){

                imageHeight=kScreenWidth/2;


            }else{

                imageHeight=kScreenWidth/3;

            }

           

            

            CGFloat  imageWidth=self.model.imagesArray.count>3?(kScreenWidth/3):(kScreenWidth/self.model.imagesArray.count);

            

            NSMutableArray  *mutableImagesFrames=[NSMutableArrayarray];

            

            [self.model.imagesArrayenumerateObjectsUsingBlock:^(id _Nonnull obj,NSUInteger idx, BOOL *_Nonnull stop) {

                

                CGRect imagerect=CGRectMake(imageWidth*(idx%3), previousHeight+(idx/3)*imageHeight, imageWidth, imageHeight);

                NSString  *string=NSStringFromCGRect(imagerect);

                [mutableImagesFrames  addObject:string];

                

             

                if (idx==_model.imagesArray.count-1) {

                    if (_model.imagesArray.count==3) {

                         previousHeight=previousHeight+kScreenWidth/3+margin;

                    }elseif(_model.imagesArray.count==2){

                        previousHeight=previousHeight+kScreenWidth/2+margin;


                    }

                    else {

                    previousHeight=previousHeight+(kScreenWidth/3*2)+margin+6;

                    }

                }

                

            }];

            


            

        _imagesArrayFrames=[NSArray arrayWithArray:mutableImagesFrames];


        }

        

        _cellHeight =previousHeight;


        

    }

    returnself;

}

@end




//

//  Model.h

//  TableViewController

//

//  Created by mac on 16/5/27.

//  Copyright © 2016 mac. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface Model : NSObject

@property(nonatomic,copy)NSString  *userName;

@property(nonatomic,copy)NSString  *userIcon;


@property(nonatomic,copy)NSString  *date;


@property(nonatomic,copy)NSString  *text;


@property(nonatomic,strong)NSArray  *imagesArray;


//@property(nonatomic,strong)NSArray  *commentArray;



-(instancetype)initWithDic:(NSDictionary*)dic;

@end







通过长时间学习  认真研究了  拖过断点加载 顺序




第一次  执行顺序

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 1;

}


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

    returnself.modelArr.count;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ModelFrame *modelFrame  = (ModelFrame*)self.modelArr[indexPath.row];

    return modelFrame.cellHeight;

}




//上面执行3次  下面Cell创建两个 (两个Cell超出屏幕一半多   ) 



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

    

     MessageTableViewCell *cell=[tableViewdequeueReusableCellWithIdentifier:@"message"];

    if (!cell) {

  //这里打了断点执行两次创建一个屏幕多的cell     

 cell=[[MessageTableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"message"];

        

    }

    cell.modelFrame=self.modelArr[indexPath.row];

    

    return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ModelFrame *modelFrame  = (ModelFrame*)self.modelArr[indexPath.row];

    return modelFrame.cellHeight;

}





拖动cell  cell不到一半距离开始重用执行下面两个方法

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

    

     MessageTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"message"];

//重用了隐藏

    cell.modelFrame=self.modelArr[indexPath.row];

    

    return cell;

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    ModelFrame *modelFrame  = (ModelFrame*)self.modelArr[indexPath.row];

    return modelFrame.cellHeight;

}


//点赞 操作思路  更新当前按钮的状态  替换当前对应的模型 更新数据源(防止下次重用的状态) 上传网络





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值