HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了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美元(没有一个工作的应用程序,甚至),@R_678_9447@有一个人在苹果衡量这一个.

有什么想法吗?

谢谢!

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

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

    NSLog(@"%@",CellIdentifier);

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

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

解决方法

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

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

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

大佬总结

以上是大佬教程为你收集整理的ios – UITableView cell.contentView.bounds.size.width更改与单元格重用全部内容,希望文章能够帮你解决ios – UITableView cell.contentView.bounds.size.width更改与单元格重用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。