如何使用 uitableviewcell 在每个 uitableview 行中创建不同数量的 uilabel?
Posted
技术标签:
【中文标题】如何使用 uitableviewcell 在每个 uitableview 行中创建不同数量的 uilabel?【英文标题】:How can I create different amount of uilabel in each uitableview row with uitableviewcell? 【发布时间】:2017-09-26 06:47:03 【问题描述】:我想在 UITableViewCell 的一行中创建不同数量的 UILabel,但我真的不知道该怎么做。如果我使用 UITableViewCell 的属性,它将在一行中只显示一个 UILabel(我使用动态原型单元格。),如果我在“cellForRowAtIndexPath”方法中创建每个标签,那么它将遇到重用问题,就像这样
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 的 receive.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];
更新:
终于解决了这个问题,在cell.contentview的addsubview前加上[cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
,不用自定义cell,只能使用UITableviewCell。
【问题讨论】:
动态创建标签时遇到什么问题。 这里是截图链接:i.stack.imgur.com/LcFYs.png 标签在重复使用单元格后会与其他标签的信息重叠。 可能是您在 heightForRowAtIndexPath 方法中没有给出正确的高度。 在我滚动视图之前,一切都很好,这个截图是我滚动回顶部之后的...... 【参考方案1】:创建UITableViewCell
的自定义子类。
【讨论】:
以上是关于如何使用 uitableviewcell 在每个 uitableview 行中创建不同数量的 uilabel?的主要内容,如果未能解决你的问题,请参考以下文章
如何加快 UITableViewCell 中的 UIImageView/UIButton?
如何根据 UITableViewCell 点击更新 UINavigationItem?
确定 UITableViewCell 中的 UIButton
如何使用 monotouch 在 uitableviewcell 中添加 uipickerview 作为子视图?
重用自定义UITableViewCell,如何获取每个单元格文本字段的文本进行保存?
如何使用 cellForRowAtIndexPath 从 TableView 中获取自定义 UITableViewCell?