UITableView cell.contentView.bounds.size.width单元重用的更改

我使用cell.contentView.bounds.size.width来计算UITableView单元格中文本字段的位置。 当单元格被创build时,debugging代码将宽度报告为302.当单元格从屏幕上滚动然后重新打开时,debugging代码每次报告它是280。 它似乎不想回到302,并停留在280.最终的结果是文本字段被放在错误的地方第二次字段放入单元格的contentView,虽然它被放在第一次正确的地方。

不知何故,我认为22号很重要,但我不知道它是什么。 猜测可能是披露箭头,我把宽度测定的前面的“清除单元格”代码,包括将附件设置为nada。

有人可以告诉我这里发生了什么事吗?

代码(无关 – 我知道的东西 – 剪掉的东西)看起来像这样:

 // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } // Configure the cell. while( [cell.contentView.subviews count] ){ id subview = [cell.contentView.subviews objectAtIndex:0]; [subview removeFromSuperview]; } cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 8< snip! CGFloat theCellWidth = cell.contentView.bounds.size.width - 44.0; CGFloat theLineHeight = [[UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0] lineHeight]; NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width); if (0==section) { switch (row) { case 2: while( [cell.contentView.subviews count] ){ id subview = [cell.contentView.subviews objectAtIndex:0]; [subview removeFromSuperview]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.textLabel.text = @" "; cell.detailTextLabel.text = @"The Age"; theAgeTextField.frame = CGRectMake(10.0, 2.0, theCellWidth, theLineHeight); // NSLog(@"cell.contentView %@",cell.contentView); theAgeTextField.text = theAge; theAgeTextField.font = [UIFont boldSystemFontOfSize: [UIFont labelFontSize]+1.0]; theAgeTextField.keyboardType = UIKeyboardTypeDecimalPad; theAgeTextField.borderStyle = UITextBorderStyleNone; theAgeTextField.userInteractionEnabled = NO; [cell.contentView addSubview:theAgeTextField]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; break; 8< snip! (lots of closing braces and other stuff omitted) return cell; } 

想在家里试试这个男孩和女孩吗?

从一个新的基于导航的应用程序开始。 把下面的代码放到RootViewController.m中:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } NSLog(@"cell.contentView.bounds.size.width %1.0f",cell.contentView.bounds.size.width); // Configure the cell. return cell; } 

实现这一点所需的默认代码只有两个更改:更改节中的行数(“返回5”)和单元格样式必须为UITableViewCellStyleSubtitle 。 然后,当你运行程序时,你会看到五行:

 2011-06-18 11:10:19.976 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 2011-06-18 11:10:19.978 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 2011-06-18 11:10:19.979 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 2011-06-18 11:10:19.980 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 2011-06-18 11:10:19.982 TestTableCells[7569:207] cell.contentView.bounds.size.width 302 

从屏幕上拖出一些单元格(上下拖动不起任何作用),当它们重新出现时,你会看到:

 2011-06-18 11:10:24.013 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 2011-06-18 11:10:24.047 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 2011-06-18 11:10:24.130 TestTableCells[7569:207] cell.contentView.bounds.size.width 320 

坦率地说,我对此感到沮丧,而且我非常想要花99美元(甚至没有一个工作的应用程序),所以我可以让苹果公司的某个人权衡这个问题。

有人有什么想法这里发生了什么?

谢谢!


想要看更有趣的东西吗? 试试这个来代替static NSString...行:

  NSString *CellIdentifier = [NSString stringWithFormat: @"%d", arc4random() ]; NSLog(@"%@",CellIdentifier); 

现在,每次,日志中的宽度始终为 302.那么,重用单元格的内容宽度可能与原始单元格不同。

再次…想知道…任何人都有线索?

我怀疑你所看到的是由于改变单元格的附件视图,最终在下一次单元格执行时会调整内容视图的大小-layoutSubviews 。 这应该发生在单元格被添加到表格之前,但在此之前内容视图的边界将不会被更新。

无论我不明白为什么这将是一个问题。 您似乎只关心设置您的theAgeTextField的宽度,所以如果您相对于内容视图的当前宽度适当地调整它的大小,并设置其autoresizingMask标志给它一个灵活的宽度,那么它应该根据需要增长或缩小,当边界的内容视图更改。

如果你需要更详细的布局行为,那么这应该可能都发生在一个UITableViewCell子类。 请参阅-prepareForReuse-layoutSubviews ,以便定制单元格及其子视图。 您的数据源应该能够将theAge传递给您的单元格子类的一个实例,而不会关心如何显示的细节。

我碰到了相同的东西,但偏移量是20而不是22.我认为约拿的答案是在正确的轨道,但一个快速的解决方法,避免cell.contentView.frame /边界的大小调整行为我只是使用cell.frame / bounds作为我在单元格的contentView中放置视图的参考。

你必须为行代理方法实现高度,并在那里做所有的计算

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { // do math return newheight; } 

它会给你你以后的东西,你也可以访问索引path,所以你可以很容易地find你的数组中的行,并做你需要的一切,否则你会从队列中获得一个重用行的大小,这是总是您的默认行大小。