iOS中的UITableView自定义Cell(模仿新浪微博)

 

 

定义一个KCStatusTableViewCell实现UITableViewCell,一般实现自定义UITableViewCell需要分为两步:第一初始化控件;第二设置数据,重新设置控件frame。原因就是自定义Cell一般无法固定高度,很多时候高度需要随着内容改变。此外由于在单元格内部是无法控制单元格高度的,因此一般会定义一个高度属性用于在UITableView的代理事件中设置每个单元格高度。

首先看一下微博模型KCStatus,这个模型主要的方法就是根据plist字典内容生成微博对象:

KCStatus.h

#import <Foundation/Foundation.h>

//微博模型KCStatus,这个模型主要的方法就是根据plist字典内容生成微博对象:

@interface KCStatus : NSObject

#pragma mark - 属性
@property (nonatomic,assign) long long Id;//微博ID
@property (nonatomic,copy) NSString *profileImageUrl;//用户头像
@property (nonatomic,copy) NSString *userName;//用户名称
@property (nonatomic,copy) NSString *mbtype;//会员类型
@property (nonatomic,copy) NSString *createdAt;//创建时间
@property (nonatomic,copy) NSString *source;//设备来源
@property (nonatomic,copy) NSString *text;//微博内容

#pragma mark - 方法
#pragma mark 根据字典初始化微博对象
-(KCStatus *)initWithDictionary:(NSDictionary *)dic;

#pragma mark 初始化微博对象(静态方法)
+(KCStatus *)statusWithDictionary:(NSDictionary *)dic;

@end

 

 

KCStatus.m

#import "KCStatus.h"

@implementation KCStatus

#pragma mark 根据字典初始化微博对象
-(KCStatus *)initWithDictionary:(NSDictionary *)dic{
    
    if (self = [super init]) {
        
        self.Id = [dic[@"id"] longLongValue];
        self.profileImageUrl = dic[@"profileImageUrl"];
        self.userName = dic[@"userName"];
        self.mbtype = dic[@"mbtype"];
        self.createdAt = dic[@"createdAt"];
        self.source = dic[@"source"];
        self.text = dic[@"text"];
        
    }
    
    return self;

}


#pragma mark 初始化微博对象(静态方法)
+(KCStatus *)statusWithDictionary:(NSDictionary *)dic{
    
    KCStatus *status = [[KCStatus alloc]initWithDictionary:dic];
    
    return status;
}


//使用 stringWithFormat 拼接设备来源
-(NSString *)source{
    return [NSString stringWithFormat:@"来自 %@", _source];
}


@end

 

 

然后看一下自定义的Cell

KCStatusTableViewCell.h

#import <UIKit/UIKit.h>
//#import "KCStatus.h"

// import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告诉你。
@class KCStatus;


//自定义的cell
@interface KCStatusTableViewCell : UITableViewCell

#pragma mark 微博对象
@property (nonatomic, strong) KCStatus *status;

#pragma mark 单元格高度
@property (assign, nonatomic) CGFloat height;


@end

 

KCStatusTableViewCell.m

#import "KCStatusTableViewCell.h"
#import "KCStatus.h"

#define KCColor(r,g,b) [UIColor colorWithHue:r/255.0 saturation:g/255.0 brightness:b/255.0 alpha:1] //颜色宏定义
#define kStatusTableViewCellControlSpacing 10 //控件间距
#define kStatusTableViewCellBackgroundColor KCColor(251,251,251)
#define kStatusGrayColor KCColor(50,50,50)
#define kStatusLightGrayColor KCColor(120,120,120)

#define kStatusTableViewCellAvatarWidth 40 //头像宽度
#define kStatusTableViewCellAvatarHeight kStatusTableViewCellAvatarWidth
#define kStatusTableViewCellUserNameFontSize 14
#define kStatusTableViewCellMbTypeWidth 13 //会员图标宽度
#define kStatusTableViewCellMbTypeHeight kStatusTableViewCellMbTypeWidth
#define kStatusTableViewCellCreateAtFontSize 12
#define kStatusTableViewCellSourceFontSize 12
#define kStatusTableViewCellTextFontSize 14



@interface KCStatusTableViewCell(){
    
    UIImageView *cellAvatar;//用户头像
    UIImageView *cellMbType;//会员类型
    UILabel *cellUserName;//用户名称
    UILabel *cellCreateAt;//创建时间
    UILabel *cellSource;//设备来源
    UILabel *cellText;//微博内容

}

@end

@implementation KCStatusTableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initSubView];
    }
    
    return self;

}


#pragma mark 初始化视图
-(void)initSubView{
    
    
    //头像控件
    cellAvatar = [[UIImageView alloc]init];
    [self.contentView addSubview:cellAvatar];
    
    //用户名
    cellUserName = [[UILabel alloc]init];
    cellUserName.textColor = kStatusGrayColor;
    cellUserName.font = [UIFont systemFontOfSize:kStatusTableViewCellUserNameFontSize];
    [self.contentView addSubview:cellUserName];
    
    //会员类型
    cellMbType = [[UIImageView alloc]init];
    [self.contentView addSubview:cellMbType];
    
    //日期
    cellCreateAt = [[UILabel alloc]init];
    cellCreateAt.textColor = kStatusLightGrayColor;
    cellCreateAt.font = [UIFont systemFontOfSize:kStatusTableViewCellCreateAtFontSize];
    [self.contentView addSubview:cellCreateAt];
    
    //设备
    cellSource = [[UILabel alloc]init];
    cellSource.textColor = kStatusLightGrayColor;
    cellSource.font = [UIFont systemFontOfSize:kStatusTableViewCellSourceFontSize];
    [self.contentView addSubview:cellSource];
    
    //内容
    cellText = [[UILabel alloc]init];
    cellText.textColor = kStatusGrayColor;
    cellText.font = [UIFont systemFontOfSize:kStatusTableViewCellTextFontSize];
    cellText.numberOfLines=0;
    //    cellText.lineBreakMode=NSLineBreakByWordWrapping;
    [self.contentView addSubview:cellText];

}


#pragma mark 设置微博
-(void)setStatus:(KCStatus *)status{
    
    //设置头像大小和位置
    CGFloat avatarX=10,avatarY=10;
    CGRect avatarRect=CGRectMake(avatarX, avatarY, kStatusTableViewCellAvatarWidth, kStatusTableViewCellAvatarHeight);
    cellAvatar.image=[UIImage imageNamed:status.profileImageUrl];
    cellAvatar.frame=avatarRect;
    
    
    //设置会员图标大小和位置
    CGFloat userNameX= CGRectGetMaxX(cellAvatar.frame)+kStatusTableViewCellControlSpacing ;
    CGFloat userNameY=avatarY;
    //根据文本内容取得文本占用空间大小
    CGSize userNameSize=[status.userName sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kStatusTableViewCellUserNameFontSize]}];
    CGRect userNameRect=CGRectMake(userNameX, userNameY, userNameSize.width,userNameSize.height);
    cellUserName.text=status.userName;
    cellUserName.frame=userNameRect;
    
    
    //设置会员图标大小和位置
    CGFloat mbTypeX=CGRectGetMaxX(cellUserName.frame)+kStatusTableViewCellControlSpacing;
    CGFloat mbTypeY=avatarY;
    CGRect mbTypeRect=CGRectMake(mbTypeX, mbTypeY, kStatusTableViewCellMbTypeWidth, kStatusTableViewCellMbTypeHeight);
    cellMbType.image=[UIImage imageNamed:status.mbtype];
    cellMbType.frame=mbTypeRect;
    
    
    //设置发布日期大小和位置
    CGSize createAtSize=[status.createdAt sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kStatusTableViewCellCreateAtFontSize]}];
    CGFloat createAtX=userNameX;
    CGFloat createAtY=CGRectGetMaxY(cellAvatar.frame)-createAtSize.height;
    CGRect createAtRect=CGRectMake(createAtX, createAtY, createAtSize.width, createAtSize.height);
    cellCreateAt.text=status.createdAt;
    cellCreateAt.frame=createAtRect;
    
    
    //设置设备信息大小和位置
    CGSize sourceSize=[status.source sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kStatusTableViewCellSourceFontSize]}];
    CGFloat sourceX=CGRectGetMaxX(cellCreateAt.frame)+kStatusTableViewCellControlSpacing;
    CGFloat sourceY=createAtY;
    CGRect sourceRect=CGRectMake(sourceX, sourceY, sourceSize.width,sourceSize.height);
    cellSource.text=status.source;
    cellSource.frame=sourceRect;
    
    
    //设置微博内容大小和位置
    CGFloat textX=avatarX;
    CGFloat textY=CGRectGetMaxY(cellAvatar.frame)+kStatusTableViewCellControlSpacing;
    CGFloat textWidth=self.frame.size.width-kStatusTableViewCellControlSpacing*2;
    CGSize textSize=[status.text boundingRectWithSize:CGSizeMake(textWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:kStatusTableViewCellTextFontSize]} context:nil].size;
    CGRect textRect=CGRectMake(textX, textY, textSize.width, textSize.height);
    cellText.text=status.text;
    cellText.frame=textRect;
    
    _height=CGRectGetMaxY(cellText.frame)+kStatusTableViewCellControlSpacing;

    
}






- (void)awakeFromNib {
    // Initialization code
}



#pragma mark 重写选择事件,取消选中
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

这是我们自定义Cell这个例子的核心,自定义Cell分为两个步骤:首先要进行各种控件的初始化工作,这个过程中只要将控件放到Cell的View中同时设置控件显示内容的格式(字体大小、颜色等)即可;然后在数据对象设置方法中进行各个控件的布局(大小、位置)。在代码中有几点需要重点提示大家:

  • 对于单行文本数据的显示调用- (CGSize)sizeWithAttributes:(NSDictionary *)attrs;方法来得到文本宽度和高度。 
  • 对于多行文本数据的显示调用- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context ;方法来得到文本宽度和高度;同时注意在此之前需要设置文本控件的numberOfLines属性为0。 
  • 通常我们会在自定义Cell中设置一个高度属性,用于外界方法调用,因为Cell内部设置Cell的高度是没有用的,UITableViewCell在初始化时会重新设置高度。

 

 

最后我们看一下自定义Cell的使用过程:

KCStatusViewController.h

#import <UIKit/UIKit.h>

@interface KCStatusViewController : UIViewController

@end

 

KCStatusViewController.m

#import "KCStatusViewController.h"
#import "KCStatus.h"
#import "KCStatusTableViewCell.h"



@interface KCStatusViewController ()<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate>{
    
    UITableView *myTableView;
    NSMutableArray *statusArray;
    NSMutableArray *statusCellsArray;//存储cell,用于计算高度
 
}

@end

@implementation KCStatusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    

    //初始化数据
    [self initData];
    
    //创建一个分组样式的UITableView
    myTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    
    //设置数据源,注意实现UITableViewDataSource协议
    myTableView.dataSource = self;
    //设置代理
    myTableView.delegate = self;

    [self.view addSubview:myTableView];
    
    
}


#pragma mark 加载数据
-(void)initData{
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"StatusInfo" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];
    
    statusArray = [[NSMutableArray alloc]init];
    statusCellsArray = [[NSMutableArray alloc]init];
    
    [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [statusArray addObject:[KCStatus statusWithDictionary:obj]];
        KCStatusTableViewCell *cell = [[KCStatusTableViewCell alloc]init];
        [statusCellsArray addObject:cell];
    
    }];
  
}


#pragma mark - 数据源方法

#pragma mark 返回分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


#pragma mark 返回每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return statusArray.count;

}

#pragma mark 返回每行的单元格(cell上面展示的内容)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"UITableViewCellIdentifierKey1";
    KCStatusTableViewCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (!cell) {
        
        cell = [[KCStatusTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    }
    
    
    
    /**
     *  这个类中需要重点强调一下:Cell的高度需要重新设置(前面说过无论Cell内部设置多高都没有用,需要重新设置),这里采用的方法是首先创建对应的Cell,然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;方法中设置微博数据计算高度通知UITableView。
     */
    
    
    //在此设置微博,以便重新布局
    KCStatus *status = statusArray[indexPath.row];
    cell.status = status;
    return cell;
    
}


#pragma mark - 代理方法

#pragma mark 重新设置单元格高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    KCStatusTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    KCStatusTableViewCell *cell = statusCellsArray[indexPath.row];
    cell.status = statusArray[indexPath.row];
    
    
    return cell.height;
  
}


#pragma mark 重写状态样式方法
-(UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleLightContent;
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

 

 

大神地址:http://www.cnblogs.com/kenshincui/

转载于:https://my.oschina.net/LBBB/blog/680044

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
源码SkyMicroblogDemo,利用自动布局模仿新浪微博的首页消息显示,可以根据文字内容和图片的张数来实现cell的自适应,发现在网上类似新浪微博带有多张图片的cell自适应的资源非常少,所以自己研究了一下写了一个Demo,初步实现了想要的效果,以后会进一步的改善的。 欢迎大家提出宝贵的意见,谢谢。 问题: 1.由于刚刚在学习自动布局不久,所以在程序Debug下运行会有很多关于约束的警告。但是具体想要的效果还是实现了。 我会在将来继续修改同时也欢迎帮忙指正错误。 2.理论上cell上的图片张数是可以不做限制的,新浪微博和微信用的都是用的一到九张图片所以我也用了九张(^__^)。就实际情况而言,由于每一条信息的图片张数可能不确定所以StoryBoard和XIB的约束我还不会写,就用了手写的约束来达到目的。 3.之前我写过一个类似的项目,但是是用frame计算每张图片的位置,通过一系列计算得到cell的高度,但是滑动起来会非常卡。现在利用约束来让系统自动计算高度流畅了许多,但是我在模拟器上运行还是会有一些卡。希望大神赐教。 关于这个Demo以后的想法: 1.现在只是做了一些页面的显示并未对图片添加点击手势,未来会添加点击手势并实现图片的相册浏览。 2.现在的文字限制只是普通的文字显示,并不包括一些表情,网络连接等。未来会逐步添加的。 3.现在并未对一些模型和方法进行封装随着功能的添加会逐步进行封装方便大家使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值