版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moeryang/article/details/52892093
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 是自动适应高度的,需要再计算一次它的高度