显示复选标记附件视图移动表格内容

我跟着苹果的文档在这里添加子视图到UITableViewcell

给单元格添加第三个标签。 问题是当我显示右侧的标准复选标记附件视图时,即使没有显示第三个标签,整个单元格内容也会左移。 下面的截图 PIC

这是我的代码:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"MyCell"; UILabel *mainLabel, *secondLabel, *thirdLabel; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; cell.accessoryType = UITableViewCellAccessoryNone; mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 2.0, 100.0, 21.0)]; mainLabel.tag = MAINLABEL_TAG; //mainLabel.font = [UIFont systemFontOfSize:14.0]; //mainLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0]; mainLabel.font = [UIFont boldSystemFontOfSize:17]; mainLabel.textAlignment = UITextAlignmentLeft; mainLabel.textColor = [UIColor blackColor]; mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; [cell.contentView addSubview:mainLabel]; secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(20.0, 22.0, 100.0, 21.0)]; secondLabel.tag = SECONDLABEL_TAG; //secondLabel.font = [UIFont systemFontOfSize:12.0]; //secondLabel.font = [UIFont fontWithName:@"Geeza Pro" size:15.0]; secondLabel.font = [UIFont systemFontOfSize:15]; secondLabel.textAlignment = UITextAlignmentLeft; secondLabel.textColor = [UIColor darkGrayColor]; secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; [cell.contentView addSubview:secondLabel]; thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(203.0, 11.0, 70.0, 21.0)]; thirdLabel.tag = THIRDLABEL_TAG; //thirdLabel.font = [UIFont systemFontOfSize:12.0]; //thirdLabel.font = [UIFont fontWithName:@"Geeza Pro Bold" size:17.0]; thirdLabel.font = [UIFont boldSystemFontOfSize:17]; thirdLabel.textAlignment = UITextAlignmentRight; thirdLabel.textColor = [UIColor darkGrayColor]; thirdLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; [cell.contentView addSubview:thirdLabel]; } else { mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG]; secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG]; thirdLabel = (UILabel *)[cell.contentView viewWithTag:THIRDLABEL_TAG]; } Car *car = [self.dataModel carAtIndex:indexPath.row]; NSString *carName = [NSString stringWithFormat:@"%@",car.deviceName]; NSString *carDetails = [NSString stringWithFormat:@"%@ at %@",car.date,car.location]; NSString *carSpeed = [NSString stringWithFormat:@"%@ km/h",car.speed]; mainLabel.text = carName; secondLabel.text = carDetails; thirdLabel.text = carSpeed; return cell; } 

更新:我根据@MattGbuild议将UIViewAutoresizingFlexibleWidth更改为UIViewAutoresizingFlexibleWidth。 现在,当一个单元格被选中时,标签不会移动到左侧,而是像下面的图片一样被部分覆盖。 在这里输入图像说明

也许这造成了我的- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ? 这是它的代码:

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if([tableView cellForRowAtIndexPath:indexPath].accessoryType == UITableViewCellAccessoryCheckmark){ [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone; } else { [tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark; } } 

我将首先回顾一下苹果公司针对iOS的桌面视图编程指南 。 它有一个名为“ 仔细观察表格视图单元格 ”的部分,该部分分解了预定义的单元格的布局。

查看你的代码你已经有足够的在你的单元格中考虑UITableViewCell的子类来产生你自己的自定义单元格。 您可以将您的单元标签的所有设置代码移到一个类中,只需关心在cellForRowAtIndexPath:方法中configuration单元即可。

但是,你的问题是关注表格视图单元格的布局。 将子视图背景设置为不同的颜色有时候很有用,所以如果我们引入其他子视图(在这种情况下为辅助视图),您可以看到哪个子视图正在使用的空间以及它如何相互作用。

要解决第三个标签文本被截断的问题,首先要将标签configuration为使用较小的字体。 我会为所有的标签做这个。 标签现在应该根据标签的宽度来调整文本。 您还应该检查您的文本alignment属性,以确认它们设置为最佳选项。

 thirdLabel.adjustsFontSizeToFitWidth = YES; thirdLabel.minimumScaleFactor = 0.4f; 

这些更改的结果可能足以解决截断并保持您的布局。