美文网首页
UITabelView非等高方式

UITabelView非等高方式

作者: 耗子撼大象 | 来源:发表于2017-02-09 16:31 被阅读0次

frame方式

  • mode部分代码
@interface XMGStatus : NSObject

/** 文字/图片数据***/
/** 图像 */
@property (nonatomic, copy) NSString *icon;

/** 昵称 */
@property (nonatomic, copy) NSString *name;

/** 正文(内容) */
@property (nonatomic, copy) NSString *text;

/** VIP */
@property (nonatomic, assign, getter=isVip) BOOL vip;

/** 配图 */
@property (nonatomic, copy) NSString *picture;


/** frame数据***/
/** 图像的frame */
@property (nonatomic, assign) CGRect iconFrame;
/** 昵称的frame */
@property (nonatomic, assign) CGRect nameFrame;
/** vip的frame */
@property (nonatomic, assign) CGRect vipFrame;
/** 正文frame */
@property (nonatomic, assign) CGRect textFrame;
/** 配图的frame */
@property (nonatomic, assign) CGRect pictureFrame;

/** cell的高度 */
@property (nonatomic, assign) CGFloat cellHeight;

@end
  • cellHeight获取方方法
        /** 图像 */
        CGFloat iconX = space;
        CGFloat iconY = space;
        CGFloat iconWH = 30;
         CGFloat space = 10;
        self.iconFrame = CGRectMake(iconX, iconY, iconWH, iconWH);
        
        /** 昵称 */
        CGFloat nameX = CGRectGetMaxX(self.iconFrame) + space;
        CGFloat nameY = iconY;
        NSDictionary *nameAtt = @{NSFontAttributeName : [UIFont systemFontOfSize:17]};
        // 计算昵称文字的尺寸
        CGSize nameSize = [self.name sizeWithAttributes:nameAtt];
        CGFloat nameW = nameSize.width;
        CGFloat nameH = nameSize.height;
        self.nameFrame = CGRectMake(nameX, nameY, nameW, nameH);
        
        /** vip */
        if (self.isVip) {
            CGFloat vipX = CGRectGetMaxX(self.nameFrame) + space;
            CGFloat vipW = 14;
            CGFloat vipH = nameH;
            CGFloat vipY = nameY;
            self.vipFrame = CGRectMake(vipX, vipY, vipW, vipH);
        }
        
        /** 正文 */
        CGFloat textX = iconX;
        CGFloat textY = CGRectGetMaxY(self.iconFrame) + space;
        CGFloat textW = [UIScreen mainScreen].bounds.size.width - 2 * space;
        NSDictionary *textAtt = @{NSFontAttributeName : [UIFont systemFontOfSize:14]};
        // 最大宽度是textW,高度不限制
        CGSize textSize = CGSizeMake(textW, MAXFLOAT);
        CGFloat textH = [self.text boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAtt context:nil].size.height;
        self.textFrame = CGRectMake(textX, textY, textW, textH);
        
        /** 配图 */
        if (self.picture) { // 有配图
            CGFloat pictureWH = 100;
            CGFloat pictureX = iconX;
            CGFloat pictureY = CGRectGetMaxY(self.textFrame) + space;
            self.pictureFrame = CGRectMake(pictureX, pictureY, pictureWH, pictureWH);
            _cellHeight = CGRectGetMaxY(self.pictureFrame) + space;
        } else {
            _cellHeight = CGRectGetMaxY(self.textFrame) + space;
        }

    }
    return _cellHeight;
}
  • cell获取高度的方式
 (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"heightForRowAtIndexPath--%zd",indexPath.row);
    XMGStatus *status = self.statuses[indexPath.row];
    return status.cellHeight;
}
  • cell 里面操作
 self.pictureImageView.hidden = NO; 
 self.pictureImageView.image = [UIImage imageNamed:status.picture];
 self.pictureImageView.hidden = YES;
 self.iconImageView.frame = self.status.iconFrame;
 self.nameLabel.frame = self.status.nameFrame;
 self.vipImageView.frame = self.status.vipFrame;
 self.text_Label.frame = self.status.textFrame;
 self.pictureImageView.frame = self.status.pictureFrame;

storyboard 方式

  • IOS8 之后只适合简单的cell


    222.png
    // self-sizing(iOS8 以后)
    // 告诉tableView所有cell的真实高度是自动计算的(根据设置的约束)
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    // 设置估算高度
    self.tableView.estimatedRowHeight = 44;
  • cell 里面操作
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pitureHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureBottom;
if (status.picture) { // 有配图
        self.pictureImageView.hidden = NO;
        self.pictureImageView.image = [UIImage imageNamed:status.picture];
        self.pitureHeight.constant = 100;
        self.pictureBottom.constant = 10;
    } else { // 无配图
        self.pictureImageView.hidden = YES;
        self.pitureHeight.constant = 0;
        self.pictureBottom.constant = 0;
    }
  • IOS8 之前的操作方式
    // 仍然需要设置估算高度 (减少tableView:heightForRowAtIndexPath:的调用次数)
    self.tableView.estimatedRowHeight = 200;
    计算cell的任务交给cell自己


    图片.png
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建一个临时的cell(目的:传递indexPath对应这行的模型,布局内部所有的子控件,得到cell的高度)
    if (cell == nil) {
        cell = [tableView dequeueReusableCellWithIdentifier:ID];
    }
    // 传递模型数据
    cell.status = self.statuses[indexPath.row];
    
    return cell.cellHeight;
}
(CGFloat)cellHeight
{
    // 强制刷新(label根据约束自动计算它的宽度和高度)
    [self layoutIfNeeded];
    
    CGFloat cellHeight = 0;
    if (self.status.picture) { // 有配图
        cellHeight = CGRectGetMaxY(self.pictureImageView.frame) + 10;
    } else { // 无配图
        cellHeight = CGRectGetMaxY(self.text_Label.frame) + 10;
    }
    return cellHeight;
}

相关文章

  • UITabelView非等高方式

    frame方式 mode部分代码 cellHeight获取方方法 cell获取高度的方式 cell 里面操作 st...

  • 自定义非等高cell

    非等高cell创建方式1:xib 1.创建自定义类,继承自UITableViewCell,勾选同时创建xib,创建...

  • iOS 非等高 Cell

    将文本内容设置为自动换行 设置了Label的左边和上边间距,将其位置定死。 Label的lines设置为0行才能正...

  • 如何做优化,UITabelView才能更加顺滑

    如何做优化,UITabelView才能更加顺滑 如何做优化,UITabelView才能更加顺滑

  • 02-UITableview(3)

    0605非自定义等高 表格刷新 1. 01-自定义非等高cell01-xib 用故事板(sb)创建的cell既不用...

  • 自定义等高和非等高cell

    等高 storyboard 拖一个tableView,设置固定的行高 在cell上设置子控件 设置cell的标识 ...

  • css实现等高布局

    等高布局是指在同一个父容器里,子元素的高度相等的布局方式,等高布局的实现有伪等高和真等高,伪等高只是在视觉上给人感...

  • UITabelView

    一、去掉tableview的线条 二、取消cell的点击状态 三、回到tabelview的顶部 四、设置tabel...

  • UItabelView头部视图;

    设置UItabelView的头部视图:

  • UITableView(五)-自定义Cell(等高及非等高)

    等高的自定义Cell通过storyboard创建步骤1.storyboard部分在storyboard中添加需要的...

网友评论

      本文标题:UITabelView非等高方式

      本文链接:https://www.haomeiwen.com/subject/ntfwittx.html