2017-09-26 47 views
0

我想在UITableViewCell的一行中创建不同数量的UILabel,但我真的不知道该怎么做。如果我使用UITableViewCell的属性,它将在一行中显示一个UILabel(即时使用动态原型单元格),如果我在“cellForRowAtIndexPath”方法中创建每个标签,则会遇到重用问题,就像这样如何在uitableviewcell的每个uitableview行中创建不同数量的uilabel?

屏幕截图screenshot

这里是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
SpreeAllOrdersTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SpreeAllOrdersTableViewCell" forIndexPath:indexPath]; 
NSString *itemName = @""; 
NSString *variantName = @""; 
NSString *quantity = @""; 
NSDictionary *tempDic; 
NSArray *orderItemsAry; 
CGFloat collectLabelHeight = 0.0; 
for (NSInteger i=0; i<aryForStatus.count; i++) { 
    if (i == indexPath.section) { 
     tempDic = [SharedFunctions getValidDictionary:[[aryForStatus objectAtIndex:i] objectAtIndex:indexPath.row]]; 
     cell.orderID.text = [SharedFunctions getValidString:[tempDic objectForKey:@"id"]]; 
     cell.requestedUserName.text = [SharedFunctions getValidString:[tempDic objectForKey:@"requesterUserName"]]; 
     itemName = [SharedFunctions getValidString:[tempDic objectForKey:@"name"]]; 
     orderItemsAry = [SharedFunctions getValidArray:[tempDic objectForKey:@"orderItems"]]; 

     for (NSUInteger a=0; a<orderItemsAry.count; a++) { 
      variantName = [[orderItemsAry objectAtIndex:a] objectForKey:@"variantName"]; 
      quantity = [[orderItemsAry objectAtIndex:a] objectForKey:@"quantity"]; 

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, cell.requestedUserName.frame.origin.y + 30 + a*40, cell.frame.size.width/2, 30)]; 

      label.text = [NSString stringWithFormat:@"%@, %@ x %@", variantName, itemName, quantity]; 
      label.textColor = [UIColor colorWithRed:74.0/255.0 green:74.0/255.0 blue:74.0/255.0 alpha:1.0]; 
      label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14]; 
      label.textAlignment = NSTextAlignmentCenter; 
      label.layer.backgroundColor = [UIColor colorWithRed:216.0/255.0 green:216.0/255.0 blue:216.0/255.0 alpha:1.0].CGColor; 
      label.layer.cornerRadius = 5.0; 
      label.layer.masksToBounds = YES; 
      [label removeFromSuperview]; 
      [cell.contentView addSubview:label]; 

      collectLabelHeight = 125 + a*40; 
     } 
     UILabel *collectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, collectLabelHeight - 10, cell.frame.size.width, 30)]; 
     collectionLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:155.0/255.0 blue:155.0/255.0 alpha:1.0]; 
     collectionLabel.font = [collectionLabel.font fontWithSize:13]; 
     if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_PURCHASE]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Deposited on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"depositedAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_DELIVERY]) { 
      if ([[SharedFunctions getValidString:[tempDic objectForKey:@"deliveryMethod"]] isEqualToString:@"MeetUp"]) { 
       collectionLabel.text = [NSString stringWithFormat:@"MeetUp @ %@", [SharedFunctions getValidString:[tempDic objectForKey:@"meetUpLocation"]]]; 
      }else { 
       collectionLabel.text = @"Courier Delivery"; 
      } 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_ACKNOWLEDGEMENT]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Dispatched on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"deliveredAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_COMPLETED]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Released Payment on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"completedAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_CANCELLED]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Cancelled on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"cancelledAt"]]]; 
     } 
     [collectionLabel removeFromSuperview]; 
     [cell.contentView addSubview:collectionLabel]; 
    } 
} 
return cell; 

}

更新:

即使我使用uitableviewcell的子类,但它不能工作。 如果我在tableviewcellcontroller的init中创建uilabel,并且它不会从uitableview中接收数据,或者我需要使用该块将数组发送到tableviewcell时使用init方法? 在此先感谢!

@implementation SpreeAllOrdersTableViewCell { 
UILabel *label; 
UILabel *collectionLabel;} 

- (void)awakeFromNib { 
[super awakeFromNib];} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated { 
[super setSelected:selected animated:animated];} 

- (void)layoutCellView { 
NSString *itemName = @""; 
NSString *variantName = @""; 
NSString *quantity = @""; 
NSDictionary *tempDic; 
NSArray *orderItemsAry; 
CGFloat collectLabelHeight = 0.0; 
for (NSInteger i=0; i<self.itemArray.count; i++) { 
    if (i == self.cellSection) { 
     tempDic = [SharedFunctions getValidDictionary:[[self.itemArray objectAtIndex:i] objectAtIndex:self.cellRow]]; 
     self.orderID.text = [SharedFunctions getValidString:[tempDic objectForKey:@"id"]]; 
     self.requestedUserName.text = [SharedFunctions getValidString:[tempDic objectForKey:@"requesterUserName"]]; 
     itemName = [SharedFunctions getValidString:[tempDic objectForKey:@"name"]]; 
     orderItemsAry = [SharedFunctions getValidArray:[tempDic objectForKey:@"orderItems"]]; 

     for (NSUInteger a=0; a<orderItemsAry.count; a++) { 
      variantName = [[orderItemsAry objectAtIndex:a] objectForKey:@"variantName"]; 
      quantity = [[orderItemsAry objectAtIndex:a] objectForKey:@"quantity"]; 

      label = [[UILabel alloc] initWithFrame:CGRectMake(20, self.requestedUserName.frame.origin.y + 30 + a*40, self.frame.size.width/2, 30)]; 

      // let background color fit the text 
      //    CGRect frame = label.frame; 
      //    frame.size.width = [label sizeThatFits:frame.size].width; 
      //    label.frame = frame; 
      //    label.lineBreakMode = NSLineBreakByCharWrapping; 
      label.text = [NSString stringWithFormat:@"%@, %@ x %@", variantName, itemName, quantity]; 
      label.textColor = [UIColor colorWithRed:74.0/255.0 green:74.0/255.0 blue:74.0/255.0 alpha:1.0]; 
      label.font = [UIFont fontWithName:@"Helvetica-Bold" size:14]; 
      label.textAlignment = NSTextAlignmentCenter; 
      label.layer.backgroundColor = [UIColor colorWithRed:216.0/255.0 green:216.0/255.0 blue:216.0/255.0 alpha:1.0].CGColor; 
      label.layer.cornerRadius = 5.0; 
      label.layer.masksToBounds = YES; 
      [self.contentView addSubview:label]; 

      collectLabelHeight = 125 + a*40; 
     } 
     collectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, collectLabelHeight - 10, self.frame.size.width, 30)]; 
     collectionLabel.textColor = [UIColor colorWithRed:155.0/255.0 green:155.0/255.0 blue:155.0/255.0 alpha:1.0]; 
     collectionLabel.font = [collectionLabel.font fontWithSize:13]; 
     if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_PURCHASE]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Deposited on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"depositedAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_DELIVERY]) { 
      if ([[SharedFunctions getValidString:[tempDic objectForKey:@"deliveryMethod"]] isEqualToString:@"MeetUp"]) { 
       collectionLabel.text = [NSString stringWithFormat:@"MeetUp @ %@", [SharedFunctions getValidString:[tempDic objectForKey:@"meetUpLocation"]]]; 
      }else { 
       collectionLabel.text = @"Courier Delivery"; 
      } 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_PENDING_ACKNOWLEDGEMENT]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Dispatched on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"deliveredAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_COMPLETED]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Released Payment on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"completedAt"]]]; 
     }else if ([[tempDic objectForKey:@"status"] isEqualToString:REQUEST_STATUS_CANCELLED]) { 
      collectionLabel.text = [NSString stringWithFormat:@"Cancelled on %@", [SharedFunctions getValidString:[tempDic objectForKey:@"cancelledAt"]]]; 
     } 
     [self.contentView addSubview:collectionLabel]; 
    } 
} 
- (void)prepareForReuse { 
[super prepareForReuse]; 
label.text = nil; 
collectionLabel.text = nil; 
[label removeFromSuperview]; 
[collectionLabel removeFromSuperview];} 

UPDATE: 由addsubview前加入[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 到cell.contentview终于解决了这个问题,它没有使用自定义单元格,只能使用的UITableViewCell。

+0

您在动态创建标签时面临什么问题。 – Yatendra

+0

这里是截图链接:https://i.stack.imgur.com/LcFYs.png – Leo

+0

标签将在重用单元格之后重叠其他标签的信息。 – Leo

回答

0

创建UITableViewCell的自定义子类。

+0

它不起作用....甚至当细胞准备重用时删除标签视图。 – Leo

+0

感谢您的帮助 – Leo

相关问题