UITableView cell.contentView.bounds.size.width使用单元重用进行更改 [英] UITableView cell.contentView.bounds.size.width Changes With Cell Reuse

查看:147
本文介绍了UITableView cell.contentView.bounds.size.width使用单元重用进行更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use cell.contentView.bounds.size.width to calculate the position of a text field in a UITableView cell. When the cell is created, debug code reports the width as 302. When the cell scrolls off the screen and then back on, the debug code reports that the it is 280--every time. It doesn't seem to want to go back to 302 and stays stuck at 280. The net result is that the text field gets put in the wrong place the second time the field is put into the cell's contentView, though it was put in the right place the first time.

我认为22在某种程度上是重要的,但我不知道它是什么。猜测它可能是披露箭头,我在宽度测定前移动了清除单元格代码,包括将附件设置为虚无..。

I figure 22 is significant somehow, but I don't know what it is. Guessing it might be the disclosure arrow, I moved the "clear the cell" code up front of the width determination, including setting the accessory to nada.

任何人都可以告诉我这里发生了什么?

Can anybody tell me what's going on here?

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

The code (with irrelevant--that I know of--stuff snipped out) looks like this:

// 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中:

Start with a new Navigation-based Application. Put the following code into 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 。然后,当你运行程序时,你会看到五行:

There are only two changes in the default code required to make this happen: changing the number of rows in the section ("return 5") and the style of the cell must be UITableViewCellStyleSubtitle. Then, when you run the program, you'll see five lines of this:

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

将一些单元格拖出屏幕(向上 - 向下拖动不做任何事情)当它们重新出现时,你得到这个:

Drag some cells off the screen (drag up--down doesn't do anything) and when they reappear, you get this:

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美元砸到(没有工作的应用程序) ,甚至)所以我可以让苹果公司的人在这一点上权衡。

Frankly, I'm frustrated as heck with this, and am so very tempted to pony up the $99 (without a working app, even) so I can have somebody at Apple weigh in on this one.

任何人都知道这里发生了什么?

Anybody got any ideas what's going on here?

谢谢!

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

Wanna' see something more interesting? Try this in place of the static NSString... line:

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

    NSLog(@"%@",CellIdentifier);

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

Now, every time, the width in the log is always 302. It would seem, then, that a reused cell has different content width than the original cell.

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

Again... wondering... anybody got a clue?

推荐答案

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

I suspect that what you are seeing is due to changing the cell's accessory view which will eventually resize the content view the next time the cell performs -layoutSubviews. That should occur when the cell is added to the table but until then the bounds of the content view will not be updated.

无论如何我不明白为什么会这样一个问题。您似乎只关心设置 theAgeTextField 的宽度,所以如果相对于内容视图的当前宽度适当调整大小并设置其 autoresizingMask flags赋予它灵活的宽度,然后当内容视图的边界发生变化时,它应该根据需要增大或缩小。

Regardless I don't see why this would be an issue. You appear to only be concerned with setting the width of your theAgeTextField so if you size it appropriately relative to the content view's current width and set its autoresizingMask flags to give it a flexible width then it should grow or shrink as needed when the bounds of the content view change.

如果你需要更详细的布局行为然后这可能都发生在 UITableViewCell 子类中。有关自定义单元格及其子视图的机会,请参阅 -prepareForReuse -layoutSubviews 。您的数据源应该只能将 theAge 传递给您的单元子类的实例,而不关心将如何显示它的详细信息。

If you need more detailed layout behavior then this should probably all occur within a UITableViewCell subclass. See -prepareForReuse and -layoutSubviews for opportunities to customize the cell and its subviews. Your datasource should be able to just pass theAge to an instance of your cell subclass and not be concerned with the details of how that will be displayed.

这篇关于UITableView cell.contentView.bounds.size.width使用单元重用进行更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆