Question

I have a table with a custom cell, in the cells are two UIViews the top one is always displayed and the bottom one is hidden.

When the cellCellForRowAtIndexPath is called the cells are all reduced in height to just show the top UIView.

When the user clicks the cell then that cell is expanded and then I unhide the second UIView. The problem is that sometimes the second UIView does not appear on first click on the cell.

Clicking the cell again makes it all disappear and then another click and it always appears perfectly. Scroll down and then pick another and first click it may not appear or will be in wrong place, two more licks and it is perfect.

I think it is a ReusableCell issue but I have no idea how to get around it.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

cellID = @"Cell";

customCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];

// Test to see if the Cell has already been built
if (cell == nil)
{
    cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}

// Check if the selected Cell is being drawn and unhide the pull down view and adjust y origin
if(indexPath.row == selectedCell)
{
     // Unhide the view
     cell.pullDownView.hidden = NO;

    CGRect tempFrame = cell.pullDownView.frame;
    tempFrame.origin.y = [[secondViewY objectAtIndex:indexPath.row] floatValue];
    cell.pullDownView.frame = tempFrame;

     // Image in second view    
     cell.mainImage2.image = [theImages objectAtIndex:indexPath.row]; 
}
else
{
    cell.pullDownView.hidden = YES;
}

// Image in first view
cell.mainImage.image = [theImages objectAtIndex:indexPath.row];




return cell;
}

So clicking a cell calls the didSelectRow which looks like this

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

if(selectedCell == indexPath.row)
{
    selectedCell = -1;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
else
{
    selectedCell = indexPath.row;
}

//    [tableView reloadData];

[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationMiddle];
[tableView endUpdates];
}

I have tried both reloadData and BeginUpdates as you can see but this still does not work. Is this because I am hiding/unhiding should I do Alpha to 0 and then 1 when selected? Is this a reusable cell thing?

Ohhh for completeness. You may notice I have a table where all the heights are different which is why I change the Y of the second UIView.

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

float returnHeightValue;

if(indexPath.row == selectedCell)
{
    returnHeightValue = [[selectedCellHeights objectAtIndex:indexPath.row] floatValue];
}
else
{
    returnHeightValue = [[normalCellHeights objectAtIndex:indexPath.row] floatValue];
}


return returnHeightValue;

}
Was it helpful?

Solution 2

Okay changed it all around as hiding and unhiding content on a cell that changed did not work so well (got artifacts left in the table and sometimes things would not show up).

I am now creating two custom cells one has all the additional objects when the cell is selected the other does not.

So far seems to be much better way of doing it.

OTHER TIPS

Okay I have changed how I address the items. I have removed the UIView (which they were all embedded in) and instead I now have in my .h of my custom cell.

@property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *pullDownItems;
@property (weak, nonatomic) IBOutlet UIImageView *mainImage;

Rather than

@property (weak, nonatomic) IBOutlet UIView *pullDownView;

Okay so now I have to unhide and move two items rather than one but that is way better than ding them individually would have been a pain (there is around 10 UILabels). I have enumerate the array like so;

    for(UILabel *obj in cell.pullDownItems)
    {
        obj.hidden = NO;
        tempFrame = obj.frame;
        tempFrame.origin.y = tempFrame.origin.y - tempYOffset;
        obj.frame = tempFrame;
    }

Any new items I just just attach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top