-(void)configueUIWithOrders:(NSMutableArray *)orders{
self.ordersCount = orders.count;
NSInteger totalPrice =0;
OrderModel *typeModel = orders[0];
if ([typeModel.orderTypeisEqualToString:@"wait"]) {
self.payTypeLabel.text =@"待支付";
}elseif ([typeModel.orderTypeisEqualToString:@"work"]){
self.payTypeLabel.text =@"服务中";
}else{
//已完成
self.payTypeLabel.text =@"已完成";
}
/************ configue子订单*****************/
for (int i =0; i <self.ordersCount; i++) {
OderSubItem * subOrder = [[[NSBundlemainBundle]loadNibNamed:@"OderSubItem"owner:selfoptions:nil]firstObject];
OrderModel *subOrderModel = orders[i];
subOrder.orderName.text = subOrderModel.orderName;
subOrder.orderDesc.text = subOrderModel.orderDesc;
//订单总价
totalPrice += subOrderModel.orderPrice;
subOrder.tag =1001 + i;
[self.orderLabelsContentaddSubview:subOrder];
//不设置为No会把AutoresizingMask自动转化成AutoLayout,必须写在添加到父View之后设置
[subOrder setTranslatesAutoresizingMaskIntoConstraints:NO];
}
//设置约束
[selfsetConstraintWithSubView];
self.ordersTotalLabel.text = [@(totalPrice)stringValue];
}
- (void)setConstraintWithSubView{
for (int i =0; i <self.ordersCount; i++) {
//在取父View上的subVIew时要以后加的View.tag的为基准,否则会造成在设置最后一个subView的Constraint的时候发现它的nextView不存在
constraint的设置则要以上一个subView为标准
UIView * nextOrderView = [self.orderLabelsContentviewWithTag:1001 + i];
UIView * lastOrderView = [[UIViewalloc]init];
NSLayoutConstraint *subOrderTop = [[NSLayoutConstraintalloc]init];
if (i ==0) {
lastOrderView = self.orderLabelsContent;
subOrderTop = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:lastOrderViewattribute:NSLayoutAttributeTopmultiplier:1constant:10.0f];
}else{
lastOrderView = [self.orderLabelsContentviewWithTag:1001 + i -1];
subOrderTop = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeToprelatedBy:NSLayoutRelationEqualtoItem:lastOrderViewattribute:NSLayoutAttributeBottommultiplier:1constant:10.0f];
}
NSLayoutConstraint *subOrderLeft = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeLeadingrelatedBy:NSLayoutRelationEqualtoItem:self.orderLabelsContentattribute:NSLayoutAttributeLeadingmultiplier:1constant:0];
NSLayoutConstraint *subOrderRight = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeTrailingrelatedBy:NSLayoutRelationEqualtoItem:self.orderLabelsContentattribute:NSLayoutAttributeTrailingmultiplier:1constant:0];
NSLayoutConstraint *subOrderHeight = [NSLayoutConstraintconstraintWithItem:nextOrderViewattribute:NSLayoutAttributeHeightrelatedBy:NSLayoutRelationEqualtoItem:nilattribute:NSLayoutAttributeNotAnAttributemultiplier:1constant:40];
[self.orderLabelsContentaddConstraints:@[subOrderLeft,subOrderRight,subOrderTop,subOrderHeight]];
}
}