سؤال

I have an issue regarding UITableViewCell's textLabel property when reselecting (touching without lifting a finger) a cell that's already selected.

I have a custom contentView being added in the background just below the textLabel that has a dynamic height (including the cell) and a background for the textLabel as well which is consistently the same height (50.0f-ish).

I'm using beginUpdates and endUpdates method to animate the selection and a frame change to prevent the textLabel from centering in the cell right after the updates occur; keeping it roughly in the same confines of the background. There's no problem when selecting different cells and the textLabel stays on the top where it should be.

The main issue right now is when the user touches their finger (without lifting it) on the selected cell again, the textLabel recenters according to the cell's height change.

    - (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;

}

I'm using iOS 3.0 so I don't think didHighlightRowAtIndexPath: will work, and I was wondering if there are any delegate methods that reset the textLabel frame after beginUpdates/endUpdates have been called.

I've logged in the frame changes and it comes up like so:

Before beginUpdates/endUpdates have been called

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

After beginUpdates/endUpdates have been called and manual frame change

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

The height shows 199 before those two methods are called, and 50 afterwards. However, the textLabel keeps on recentering vertically in the cell whenever the selected cell is reselected, regardless of the forced frame change.

If anyone can help I'd be greatly appreciated.

Link to picture example -> http://i50.tinypic.com/2r6o5k5.jpg

هل كانت مفيدة؟

المحلول

You are changing the height of UILabel when cell is selected right, then add a condition before you change the UILabel height.

i.e initially selectedIndexRow will be -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

        }
    }

As you are checking for selectedIndex add above condition too.. You don't need to change much of your code..

نصائح أخرى

Try this one:

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

You have to increase cell height because you are using default label not custom

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top