ios – 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美元(没有一个工作的应用程序,甚至),所以我可以有一个人在苹果衡量这一个.

有什么想法吗?

谢谢!

想看看更有趣的东西?尝试这个代替静态NSString …行:

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

    NSLog(@"%@",CellIdentifier);

现在,每一次,日志中的宽度总是为302.那么一个重用的单元格似乎与原始单元格的内容宽度不同.

再一次…想知道有人得到了一个线索吗?

解决方法

我怀疑你所看到的是由于更改单元格的附件视图,最终会在下一次单元格执行时调整内容视图的大小–layoutSubviews.当单元格添加到表中但直到内容视图的边界将不会更新时,才会发生这种情况.

不管我不明白为什么会是一个问题.你似乎只关心设置你的theAgeTextField的宽度,所以如果你适当地相对于内容视图的当前宽度来设置它的大小,并设置它的autoresizingMask标志给它一个灵活的宽度,那么它应该根据需要增长或缩小内容视图更改.

如果您需要更详细的布局行为,那么这应该可能都在UITableViewCell子类中.请参阅-prepareForReuse和-layoutSubviews,了解定制单元格及其子视图的机会.您的数据源应该能够将TheAge传递给您的单元格子类的实例,而不必关注如何显示的细节.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...