【问题标题】:UITableViewCell textLabel height resets after selecting/highlighting same cellUITableViewCell textLabel 高度在选择/突出显示相同的单元格后重置
【发布时间】:2013-03-09 00:22:47
【问题描述】:

在重新选择(触摸而不抬起手指)已选择的单元格时,我遇到了关于 UITableViewCell 的 textLabel 属性的问题。

我在 textLabel 正下方的背景中添加了一个自定义 contentView,它具有动态高度(包括单元格)和 textLabel 的背景,其高度始终相同(50.0f-ish)。

我正在使用 beginUpdates 和 endUpdates 方法来动画选择和框架更改,以防止 textLabel 在更新发生后立即在单元格中居中;将其大致保持在背景的相同范围内。选择不同的单元格时没有问题,并且 textLabel 保持在应该在的顶部。

现在的主要问题是当用户再次用手指(不抬起手指)触摸所选单元格时,textLabel 会根据单元格的高度变化而居中。

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        DetailView *detailView = // Create detail view

        // Add UIImageView from content to cell --------------------
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

        // Change textLabel colors, fontSize, and shadow properties
        // Create CustomContentView *ccView = nil;


        // ------
        // Remove / Hide any existing CustomContentViews
        // --- Code here
        // -------

        switch (hasImageInCustomView) {
        case YES:
            image = _imageInCustomView;
            image = NO;

            if (selectedIndexPath.row != indexPath.row) {
                [tableView beginUpdates];
                [tableView endUpdates];
            }

            CGRect frame = cell.textLabel.frame;
                    frame.size.height = 50.0f;
                    cell.textLabel.frame = frame;


            return;
        break;
        default:
            image = [detailView getImage];
        break;
        }

        // ----
        // Calculate height for image and add that to heightForExpandedRow property
        // -- Code here...
        // ----

        [tableView beginUpdates];
        [tableView endUpdates]; 

        // Add subview with UIImageView
        customContentView = [[CustomContentView alloc] initWithFrame:(CGRect){0, 0, kWidth, dynamicExpandedRow}];
        contentView.clipsToBounds = YES;
        [cell.contentView addSubview:customContentView];
        [cell.contentView sendSubviewToBack:customContentView];
        [customContentView setContentImage:image];

        CGRect frame = cell.textLabel.frame;
        frame.size.height = 50.0f;
        cell.textLabel.frame = frame;

}

我使用的是 iOS 3.0,所以我认为 didHighlightRowAtIndexPath: 不会起作用,我想知道在调用 beginUpdates/endUpdates 后是否有任何委托方法重置 textLabel 框架。

我已登录框架更改,结果如下:

在 beginUpdates/endUpdates 被调用之前

<UILabel: 0x4e55500; frame = (10 0; 250 199); text = 'Some Text Goes Here...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4e55570>>

在调用 beginUpdates/endUpdates 并手动更改帧之后

<UILabel: 0x4e55500; frame = (10 0; 250 50); text = 'Some Text Goes Here...'; clipsToBounds = YES; opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x4e55570>>

调用这两个方法之前的高度显示为 199,之后显示为 50。 但是,无论何时重新选择选定的单元格,textLabel 都会在单元格中保持垂直居中,而不管强制帧更改如何。

如果有人能提供帮助,我将不胜感激。

链接到图片示例 -> http://i50.tinypic.com/2r6o5k5.jpg

【问题讨论】:

  • 我倾向于继承 UITableViewCell 并使用 layoutSubviews,基于相关问题。
  • 遗憾的是,子类化 UITableViewCell 和使用 layoutSubviews 不起作用。当用户重新选择选定的单元格时,问题仍然存在,并且 textLabel 的尺寸被重置以适应单元格的新展开高度,同时垂直居中文本。

标签: iphone uitableview height didselectrowatindexpath textlabel


【解决方案1】:

当单元格被选中时,您正在更改UILabel 的高度,然后在更改 UILabel 高度之前添加一个条件。

即 最初 selectedIndexRow 将为 -1。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//Check if we are selecting the same cell
        if(selectedIndexRow != indexPath.row){
           if(selectedIndexRow != -1){

        //deselect the previous cell
        }

            selectedIndexRow = indexPath.row;

        //preform your action

        }
    }

当您检查 selectedIndex 时,也添加上述条件..您不需要更改太多代码..

【讨论】:

  • 好吧,当单元格高度扩展时,我试图将 textLabel 固定在顶部(除了字体大小和颜色之外没有太多的视觉变化)。
  • @Bill 你可以添加快照吗?这样它会帮助我们
  • 我发布了图片链接,因为我还没有足够的声望来发布图片。
  • 好的。我明白了,如果它是同一行,我应该取消选择该行吗?编辑:我刚刚尝试取消选择该行,但结果相同。
  • @Bill 我提到如果单元格已被选中,则不要对单元格执行取消选择操作。好的,这是我为你写的链接。 ziddu.com/download/21842790/expandableTableview.zip.html
【解决方案2】:

试试这个:

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}

您必须增加单元格高度,因为您使用的是默认标签而不是自定义标签

【讨论】:

    猜你喜欢
    • 2012-06-13
    • 2015-11-24
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 2014-06-13
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多