如何使用uitableviewcell在每个uitableview行中创建不同数量的uilabel?

时间:2017-09-26 06:47:03

标签: ios uitableview

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

SCREENSHOT 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接收.array数据,或者当我使用init方法时我需要使用该块将数组发送到tableviewcell? 提前致谢!!

@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];}

更新: 最后通过在combine.contentview的addsubview之前添加[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)]; 来解决这个问题,并且它不必使用自定义单元格,只能使用UITableviewCell。

1 个答案:

答案 0 :(得分:0)

创建UITableViewCell的自定义子类。