UITableView /cell上的button,动态配置tableView的cell的高度(效果为点击加载更多,cell加载所有内容,反之,只加载3行)

来源:互联网 发布:mac怎么玩qq游戏大厅 编辑:程序博客网 时间:2024/05/03 15:31

思路:因为自定义cell上有button,点击这个button  加载更多,我们就可以把每个cell  赋值为0 。如果点击了一个cell上的button,就把那行cell改成1.用数组装入每个cell上的标记(0和1),依据它的标记改变当行cell 的frame;

1.首先给给UIView添加一个类目,这个类目会大大方便于对于各个视图位置坐标的计算。

     添加一个继承于view 的类目,

如图:

下面是添加的方法

     在.h 文件里面,声明方法,如下:

/**
 * 获得当前视图的结束点坐标 值为 frame 的 y + height
 */
- (CGFloat)endY;

/**
 * 获得当前视图的结束点坐标 值为 frame 的 x + width
 */
- (CGFloat)endX;

/**
 * 获得当前视图的X坐标体系
 */
- (CGFloat)X;
- (CGFloat)x;

/**
 * 获得当前视图的Y坐标体系
 */
- (CGFloat)Y;
- (CGFloat)y;

/**
 * 获得当前视图的高度
 */
- (CGFloat)H;
- (CGFloat)h;

/**
 * 获得当前视图的宽度
 */
- (CGFloat)W;
- (CGFloat)w;

/**
 *   设置View的大小
 *   @param size 大小参数
 *   @param width 宽度
 *   @param heith 高度
 */
- (void)setW:(float)width;
- (void)setH:(float)height;
- (void)setW:(float)width andH:(float)height;
- (void)setWH:(CGSize)size;

/**
 *    设置View的位置
 *    @param position 设置view的位置
 *    @param x X坐标
 *    @param y Y坐标
 */
- (void)setX:(float)x;
- (void)setY:(float)y;
- (void)setX:(float)x andY:(float)y;
- (void)setXY:(CGPoint)point;

在.m 文件里面 
实现

 - (CGFloat)endY {
    return self.frame.origin.y + self.frame.size.height;
}

- (CGFloat)endX {
    return self.frame.origin.x + self.frame.size.width;
}

- (CGFloat)X {
    return self.frame.origin.x;
}
- (CGFloat)x {
    return self.X;
}

- (CGFloat)Y {
    return self.frame.origin.y;
}
- (CGFloat)y {
    return self.Y;
}

- (CGFloat)H {
    return self.frame.size.height;
}

- (CGFloat)h {
    return self.H;
}

- (CGFloat)W {
    return self.frame.size.width;
}

- (CGFloat)w {
    return self.W;
}

- (void)setW:(float)width
{
    CGRect frame = [self frame];
    frame.size.width = round(width);
    [self setFrame:frame];
}
- (void)setH:(float)height{
    CGRect frame = [self frame];
    frame.size.height = round(height);
    [self setFrame:frame];
}

- (void)setW:(float)width andH:(float)height
{
    [self setWH:CGSizeMake(width, height)];
}

- (void)setWH:(CGSize)size
{
    CGRect frame = [self frame];
    frame.size.width = round(size.width);
    frame.size.height = round(size.height);
    [self setFrame:frame];
    
}


- (void)setX:(float)x
{
    [self setX:x andY:self.frame.origin.y];
}

- (void)setY:(float)y
{
    [self setX:self.frame.origin.x andY:y];
}

- (void)setX:(float)x andY:(float)y
{
    CGRect frame = [self frame];
    frame.origin.x = round(x);
    frame.origin.y = round(y);
    [self setFrame:frame];
}

- (void)setXY:(CGPoint)point
{
    [self setX:point.x andY:point.y];
}

 现在一个UIView的类目已经完成

在进行动态匹配高度之前,宏定义一些常用的方法   声明下面要用的属性

#import <UIKit/UIKit.h>
#import "CYYMessageTableViewCell.h"//  这个是自定义cell的类。后面会用到 
#define MainScreen_Width      [UIScreen mainScreen].bounds.size.width
#define MainScreen_heigh      [UIScreen mainScreen].bounds.size.height
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define color(r,g,b)   [UIColor colorWithRed:(r/255.0) green:g/255.0 blue:(b/255.0) alpha:1.0]
#define Fmt(fmt,...)        ([NSString stringWithFormat:fmt,##__VA_ARGS__])

@interface ViewController : UIViewController<UITableViewDataSource , UITableViewDelegate , CYYMessageTablecellDelegate>{
    UITableView *table;
    NSMutableArray *state_arr;    
}
@end

在viewcontroller.m方法里面  配置tableview  

   table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, MainScreen_Width, MainScreen_heigh - 64)];
    table.backgroundColor = color(231, 231, 231);
    
    table.delegate = self;
    table.dataSource = self;
 
    table.separatorStyle  = UITableViewCellSeparatorStyleNone;//
    [self.view addSubview:table];
    
    state_arr = [[NSMutableArray alloc] init];//这是标记  初始标记每行为0
    for (int i = 0; i < 10 ; i ++ ) {
        [state_arr addObject:@"0"];
    }

下面是他的协议方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

    //CYYMessageTableViewCell为自定义cell类

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellID = @"cell";
    CYYMessageTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[CYYMessageTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    NSString *str = @"" ;

  //这是拼接字符串   

    for (int i = 0 ; i < indexPath.row; i ++) {
        str = Fmt(@"%@。。123142313232132123",str);
    }
    NSString *type =[state_arr objectAtIndex:indexPath.row];
    
    [cell setlayoutCell:str and: [type intValue]];  //此方法 是传入字符串到label  并判断 他的标志是0/1   具体实现在cell类里面

    cell.delegate        = self;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle  = UITableViewCellSelectionStyleNone;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return  cell.frame.size.height;
}

//这是cell 的代理方法的实现    因为cell上有button  ,自定义cell类 本身 无法辨别 这是哪一行的buttn  所以要用代理。block 传值也可以。不过block 消耗内存较大。

- (void)moreMessage:(CYYMessageTableViewCell *)cell{
    NSIndexPath *index = [table indexPathForCell:cell];
    [state_arr replaceObjectAtIndex:index.row withObject:@"1"];//改变当行的标志为1
    
    [table reloadRowsAtIndexPaths:[NSArray arrayWithObjects:index,nil] withRowAnimation:UITableViewRowAnimationNone];//刷新当行  withRowAnimation :后接动画类型
}


  下面就是自定义cell类   就是 CYYMessageTableViewCell

  在CYYMessageTableViewCell  .h 文件里 面

   #import <UIKit/UIKit.h>
#define MainScreen_Width      [UIScreen mainScreen].bounds.size.width

@class CYYMessageTableViewCell;
@protocol CYYMessageTablecellDelegate <NSObject>

- (void)moreMessage:(CYYMessageTableViewCell *)cell;

@end
@interface CYYMessageTableViewCell : UITableViewCell
@property (nonatomic , strong) UIImageView *title_imgView;
@property (nonatomic , strong) UIImageView *bacak_imgView;
@property (nonatomic , strong) UILabel     *message_lable;
@property (nonatomic , strong) UIView      *back_view;   //背景view
@property (nonatomic , strong) UILabel     *title_lable;     //
@property (nonatomic , strong) UILabel     *date_lable;    //装入内容 的label  此label 将要动态显示
@property (nonatomic , strong) UIButton    *more_btn;     //点击加载更多的button
@property (assign) id<CYYMessageTablecellDelegate>delegate;
- (void)setlayoutCell:(NSString *)dic and:(int)type;
@end

在.m 文件里面 引入uiview 的类目 #import "UIView+Extr.h"

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

        [self.contentView addSubview:self.title_imgView];
        [self.contentView addSubview:self.bacak_imgView];
        [self.contentView addSubview:self.back_view];
        [self.back_view addSubview:self.title_lable];
        [self.back_view addSubview:self.date_lable];
        [self.back_view addSubview:self.message_lable];

    }
    return self;
}
- (UIImageView *)title_imgView{
    if (!_title_imgView) {
        _title_imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"message_logo"]];
        _title_imgView.frame = CGRectMake(10 , 10 , 30, 30);
        [self radiusView:_title_imgView];
    }
    return _title_imgView;
}
- (UIImageView *)bacak_imgView{
    if (!_bacak_imgView) {
        UIImage *image = [[UIImage imageNamed:@"pop_box"] stretchableImageWithLeftCapWidth:20 topCapHeight:30];
        _bacak_imgView = [[UIImageView alloc] initWithImage:image];
        _bacak_imgView.frame = CGRectMake(self.title_imgView.endX + 5 , 10 , MainScreen_Width - self.title_imgView.endX - 5 - 20, 80);
    }
    return _bacak_imgView;
}
- (UIView *)back_view{
    if (!_back_view) {
        _back_view = [[UIView alloc] initWithFrame:_bacak_imgView.frame];
    }
    return _back_view;
}
- (UILabel *)title_lable{
    if (!_title_lable) {
        _title_lable = [[UILabel alloc] initWithFrame:CGRectMake(20, 3, _back_view.W - 40, 20)];
        _title_lable.text          = @"这是标题";
        _title_lable.font          = [UIFont systemFontOfSize:14];
        _title_lable.textAlignment = NSTextAlignmentLeft;
    }
    return _title_lable;
}
- (UILabel *)date_lable{
    if (!_date_lable) {
        _date_lable = [[UILabel alloc] initWithFrame:CGRectMake(20, _title_lable.endY, _title_lable.W, 17)];
        _date_lable.text          = @"2015/05/22";
        _date_lable.textAlignment = NSTextAlignmentLeft;
        _date_lable.font          = [UIFont systemFontOfSize:12];
        _date_lable.textColor     = [UIColor grayColor];
    }
    return _date_lable;
}
- (UILabel *)message_lable{
    if (!_message_lable) {
        _message_lable = [[UILabel alloc] initWithFrame:CGRectMake(20, _date_lable.endY, _title_lable.W, 17)];

        _message_lable.textAlignment = NSTextAlignmentLeft;
        _message_lable.numberOfLines = 0;
        _message_lable.font          = [UIFont systemFontOfSize:14];

    }
    return _message_lable;
}
- (CGSize)getLableHeight:(float)cpmyemyWidth fontType:(UIFont *)font lable:(NSString *)textlable{//此方法是计算label上的内容的 frame
    NSDictionary *attritute = @{NSFontAttributeName:font};
    CGSize retSize = [textlable boundingRectWithSize:CGSizeMake(cpmyemyWidth, 800)
                                             options: (NSStringDrawingTruncatesLastVisibleLine
                                            | NSStringDrawingUsesLineFragmentOrigin
                                            | NSStringDrawingUsesFontLeading)
                                          attributes:attritute context:nil].size;
    return retSize;
    
}

//这是头像圆角化的设置

- (void)radiusView:(UIView *)view{
    view.layer.masksToBounds = YES;
    view.layer.cornerRadius = 15;
    view.layer.borderWidth = 1.0;
    view.layer.borderColor = [[UIColor clearColor] CGColor];
}

//这是方法 目的 是传入 字符串 判断它显示的frame  并依据传入的0/1  设置它改怎样动态显示

- (void)setlayoutCell:(NSString *)dic and:(int)type{
    _title_lable.text = @"海贼王";
    _date_lable.text  = @"2015/5/22";
    _message_lable.text          = dic;
    switch (type) {
        case 1: //显示所有内容
        {
            [self settextNomore];break;
        }
        case 0:{ //隐藏大于三行的内容
            [self setTextInfo]; break;
        }
        default:
            break;
    }
   
    
    CGRect frame = self.frame;
    frame.size.height   = _bacak_imgView.endY + 10;
    [self setFrame:frame];
}
- (void)setTextInfo{
    //传入字体 传入字符串  传入label 高度  计算需要的大小
    CGSize  lableSize            = [self getLableHeight:_message_lable.W fontType:_message_lable.font lable:_message_lable.text];
    CGSize retSize               = [self getLableHeight:_message_lable.W fontType:_message_lable.font lable:@"123"];

    if (lableSize.height > (retSize.height * 3)) { // 这个方法的目的是让label只最多显示3行  超出三行的不显示  用省略号代替
        _message_lable.frame        = CGRectMake(20, _date_lable.endY, _title_lable.W,retSize.height*3);
        if (![_back_view viewWithTag:999]) {
            [_back_view addSubview:self.more_btn];
        }
        _back_view.H     = self.more_btn.endY + 10;
        _bacak_imgView.H = self.more_btn.endY + 10;
        
    }else{
        _message_lable.frame        = CGRectMake(20, _date_lable.endY, _title_lable.W,lableSize.height) ;
        _back_view.H     = _message_lable.H + 10;
        _bacak_imgView.h = _message_lable.endY + 10 ;
        [_more_btn removeFromSuperview];
    }
}

//这个显示实际label 的大小  

- (void)settextNomore{
    CGSize  lableSize            = [self getLableHeight:_message_lable.W fontType:_message_lable.font lable:_message_lable.text];
    
    _message_lable.frame        = CGRectMake(20, _date_lable.endY, _title_lable.W,lableSize.height) ;
    _back_view.H     = _message_lable.endY + 10;
    _bacak_imgView.h = _message_lable.endY + 10 ;
    [_more_btn removeFromSuperview];
}
- (UIButton *)more_btn{
    if (!_more_btn) {
        _more_btn = [[UIButton alloc] initWithFrame:CGRectMake(_message_lable.endX - 60, _message_lable.endY , 80, 30)];
        [_more_btn setTitle:@"显示更多" forState:0];
        [_more_btn setTitleColor:[UIColor blueColor] forState:0];
        _more_btn.titleLabel.font = [UIFont systemFontOfSize:14];
        _more_btn.titleLabel.textAlignment = NSTextAlignmentLeft;
        _more_btn.tag                      = 999;
        [_more_btn addTarget:self action:@selector(btnAction:) forControlEvents:1 << 6];
    }
    return _more_btn;
}
- (void)btnAction:(UIButton *)sender{
    [self.delegate moreMessage:self];
}
 可能 看到这里,读者对cell  的布局有点模糊   下面有图

点击 显示更多  会加载全部内容。end 。

感谢亲看到这里。

源码 :  https://git.oschina.net/chanchan/httpservice/attach_files




0 0
原创粉丝点击