iOS自动布局和UITableViewCell

1、自动布局

  • 一个UI控件使用自动布局可以只设置上边距(Top space)、下边距(Bottom speace)、左边距(Leading Space)、右边距(Trailing space);
  • 对齐一般是要同时选中两个控件(commond+鼠标)
  • UILabel 如果要自动换行需要设置Lines属性为0,上下左右边距固定
  • 如果自动约束设置错误,编译后提示窗口会告诉你时那个控件有问题

2、自定义UITableViewCell

  • 默认cell的设计视图宽度是320pt
  • 如果在
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    postCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"postCell"];
    if (cell ==nil ){
     cell = [[[NSBundle mainBundle] loadNibNamed:@"postCell" owner:self options:nil] objectAtIndex:0];
}
     NSInteger row = indexPath.row;
     NSMutableDictionary *d = [aryData objectAtIndex:row];

     cell.lbCreateTime.text = [d valueForKey:@"createtime"];
     cell.lbNickName.text     = [d valueForKey:@"createtime"] ;
     cell.lbUserGroupName.text  = [d valueForKey:@"usergroup"] ;
     cell.lbAgeAndCountryAndCity.text = [NSString stringWithFormat:@"%@ %@ %@",[d valueForKey:@"age"], [d valueForKey:@"country"],[d valueForKey:@"city"],nil];
    cell.lbContent.numberOfLines = 0;
    cell.lbContent.lineBreakMode = NSLineBreakByClipping;
    cell.lbContent.text = [d valueForKey:@"content"];
    cell.imgContent.image = [UIImage imageNamed:@"heaerpic.jpg"];



    return cell;


}

从xib里面创建cell不需要再使用initWithStyle方法创建,类似这样:

    if (cell == nil) {

            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"doctorView" owner:self options:nil];
            id oneObject = [nib objectAtIndex:0];
            cell         = [(dotorCell *)oneObject initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        }

这样的代码会导致cell宽度固定为320pt,没法适配ipone6s

  • 如果cell高度没法自动计算就需要手工计算
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    //return UITableViewAutomaticDimension;


    NSMutableDictionary *d = [aryData objectAtIndex:indexPath.row];
    postCell *cell = (postCell *)self.prototypeCell;
    //获得当前cell高度
    CGRect frame = [cell frame];
    //文本赋值
    cell.lbContent.text = [d valueForKey:@"content"];
    //设置label的最大行数
    cell.lbContent.numberOfLines = 0;
    CGSize size = CGSizeMake(300, 1000);

    CGSize labelSize = [cell.lbContent.text sizeWithFont:cell.lbContent.font constrainedToSize:size lineBreakMode:NSLineBreakByClipping];
    //NSLog(@"height is %f",cell.lbContent.frame.size.height+cell.lbContent.frame.origin.y);
    //return cell.lbContent.frame.size.height+cell.lbContent.frame.origin.y;
    return labelSize.height+cell.lbContent.frame.origin.y+cell.imgContent.frame.size.height+cell.btnLike.frame.size.height+5;
    //return 180.0f;

}

注意如果UILabel 是自动适应高度的,需要再计算一次它的高度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值