我正在尝试根据同一行上另一个标签的内容动态设置标签宽度。
我正在“cellForRowAtIndexPath”中实现逻辑
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"rowNumber:%d", indexPath.row);
EntityTableViewCell *cell = nil;
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.companyNameLabel.text = [group1 objectAtIndex:indexPath.row];
cell.companyCROfficeAddrLabel.text = [group2 objectAtIndex:indexPath.row];
cell.companyBusinessAddrLabel.text = [group3 objectAtIndex:indexPath.row];
cell.timestamp.text = [Utils getDateTimeString:[group4 objectAtIndex:indexPath.row]];
[cell.companyLogoImageView setImageWithURL:[NSURL URLWithString:[myArray objectAtIndex:indexPath.row] ]
placeholderImage:[UIImage imageNamed: @"profile-image-placeholder"]
options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
//logics to dynamically change the label width to maximum utilize the realstate
CGFloat timeStampWidth = [cell.timestamp.text sizeWithFont:cell.timestamp.font].width;
CGFloat ksCompanyNameLableMaxWidth = 235;
NSLog(@"timeStampWidth:%f", timeStampWidth);
CGSize companyNameLableSize = CGSizeMake((ksCompanyNameLableMaxWidth - timeStampWidth), cell.companyNameLabel.frame.size.height);
CGRect newFrame = cell.companyNameLabel.frame;
newFrame.size = companyNameLableSize;
cell.companyNameLabel.frame = newFrame;
NSLog(@"companyLableWidth:%f", newFrame.size.width);
return cell;
}
此代码存在多个问题。
随着表的初始化,尽管对每个单元格都调用了这段代码。标签的宽度仍然设置为故事板中的大小。另一方面,如果我向下滚动,标签会按照代码中的设计正确显示。
因为我的应用程序中还有一个 tabView 控制器,所以每当我在选项卡之间切换时,动态填充的标签宽度都会再次更新为情节提要中设置的默认标签宽度。
有人可以告诉我我做错了什么并提出一些解决方案吗?
谢谢