UITableView cell.contentView.bounds.size.width随单元格重用而更改



我使用cell.contentView.bounds.size.width来计算UITableView单元格中文本字段的位置。创建单元时,调试代码将宽度报告为302。当单元格从屏幕上滚动下来,然后再滚动回来时,调试代码每次都会报告它是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;
}

实现这一点只需要对默认代码进行两项更改:更改节中的行数("return 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。我认为Jonah的答案是正确的,但为了避免cell.contentView.frame/bounds的大小调整行为,我只使用cell.frame/bondes作为在单元格的contentView中布局视图的参考。

您必须实现行高度委托方法,并在中进行所有计算

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

它会给你想要的东西,你也可以访问索引路径,所以你可以很容易地在数组中找到行并完成你需要的一切,否则你会从队列中获得重用行的大小,这始终是你的默认行大小。

相关内容

最新更新