ios - UITableView cell.contentView.bounds.size.width 随着 Cell 重用的变化

标签 ios uitableview

我使用 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 美元(甚至没有可用的应用程序),这样我就可以让 Apple 的人参与其中。

有人知道这里发生了什么吗?

谢谢!


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

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

    NSLog(@"%@",CellIdentifier);

现在,每次,日志中的宽度总是 302。这样看来,重用单元格的内容宽度与原始单元格不同。

再次……想知道……有人知道吗?

最佳答案

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

无论如何,我不明白为什么这会成为一个问题。您似乎只关心设置 theAgeTextField 的宽度,因此如果您相对于内容 View 的当前宽度适当调整它的大小并设置其 autoresizingMask 标志以使其具有灵 active 宽度,那么当内容 View 的边界发生变化时,它应该根据需要增大或缩小。

如果您需要更详细的布局行为,那么这应该全部发生在 UITableViewCell 子类中。请参阅 -prepareForReuse-layoutSubviews 以获取自定义单元格及其 subview 的机会。您的数据源应该能够将 theAge 传递给您的单元格子类的一个实例,而不用关心它将如何显示的细节。

关于ios - UITableView cell.contentView.bounds.size.width 随着 Cell 重用的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6272009/

相关文章:

uitableview - iOS 8 设置 setSeparatorInset 为零

ios - 下载视频并将其保存到相机胶卷

ios - 如何在 Swift 中存储/检索 print() 的输出?

ios - 无法使用类型为 '*' 的参数列表调用 '(NSURL, *: NSString?)'

ios - 如何使用 Swift3 使用从 Firestore 集合中检索到的数据填充 TableView

ios - 检测文本字段应该从TableViewController中的Custom UITableViewCell返回以添加新行

ios - iphone 搜索本地 xml

ios - UIAlertController 背景色 iOS10

ios - uilabel sizetofit 不工作 uitableviewcell

ios - 具有动态 UILabel 的 UITableViewCell 的动态高度