选择/突出显示相同单元格后,UITableViewCell textLabel高度重置

时间:2013-03-20 03:05:14

标签: iphone uitableview height didselectrowatindexpath textlabel

在重新选择(不用手指触摸)已经选择的单元格时,我遇到了有关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都会在单元格中垂直重新定位。

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

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

2 个答案:

答案 0 :(得分:0)

当您正确选择单元格时,您正在更改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时也添加以上条件..您不需要更改大部分代码..

答案 1 :(得分:0)

试试这个:

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

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

相关问题