Display a UIView within a UITableViewCell pragmati

2019-09-22 08:00发布

问题:

I am using the following code to instance a UIView from a XIB and then add it to the selected UITableViewCell for displaying. When I run the code and touch the cell, I can walk through the code and see that everything is instanced correctly (nothing is nil) and yet the view is never displayed.

I have a UIView with a couple of buttons on it. In Interface Builder I set the UIView to use a sub-class of UIView which at the moment does not have any code in it, other than the boiler plate generated by Xcode. I'm hoping someone can point out any obvious errors I've made in using this code to get this to work.

Please note that at one point I had the UIView showing within the UITalbeViewCell, but I had messed some stuff up during some refactoring and ended up re-writing the code to handle this. When that happened, I could no longer display the UIView within the cell.

@implementation HZRunwayViewController
{
    EKEvent *currentEvent;
    BOOL editingEvent;
    HZEventDrawerView *eventDrawer;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Check if we are touching an event
    if (indexPath.section == 0 && [self.eventsForCurrentDay count]) {

        // Grab the selected cell and make sure it's an event cell
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        if ([cell isKindOfClass:[HZEventTableViewCell class]]) {

            // Setup our event cell and our action drawer for use.
            HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;

            if (!eventDrawer) {
                eventDrawer = (HZEventDrawerView *)[[NSBundle mainBundle] loadNibNamed:@"HZEventDrawerView" owner:self options:nil][0];
            }

            eventDrawer.bounds = eventCell.bounds;
            NSLog(@"X:%f  Y:%f  Width: %f  Height: %f", eventDrawer.bounds.origin.x, eventDrawer.bounds.origin.y, eventDrawer.bounds.size.width, eventDrawer.bounds.size.height);
            [eventCell.contentView addSubview:eventDrawer];
            [eventCell bringSubviewToFront:eventDrawer];
            eventDrawer.hidden = NO;
        }
    }
}

Update

In order to test and see if it was my XIB file or sub-class causing the issue, I stopped using it, instanced a UIView, added a UIButton to it and then added it to the Cell.contentView subview. When I touch the cell, nothing happens.

// Grab the selected cell and make sure it's an event cell
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        if ([cell isKindOfClass:[HZEventTableViewCell class]]) {

            // Setup our event cell and our action drawer for use.
            HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;

            if (!eventDrawer) {
                eventDrawer = [[UIView alloc] initWithFrame:eventCell.bounds];
                UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(5, 5, 25, 44)];
                [eventDrawer addSubview:button];
            }

            eventDrawer.frame = eventCell.bounds;
            NSLog(@"X:%f  Y:%f  Width: %f  Height: %f", eventDrawer.bounds.origin.x, eventDrawer.bounds.origin.y, eventDrawer.bounds.size.width, eventDrawer.bounds.size.height);
            [eventCell.contentView addSubview:eventDrawer];
            [eventCell bringSubviewToFront:eventDrawer];
            eventDrawer.hidden = NO;
        }

回答1:

Instead of eventDrawer.bounds = eventCell.bounds; try eventDrawer.frame = eventCell.bounds;

The bounds of a view are viewed from inside, so the origin will be 0, 0. However the frame of a view is relative to it's superview, so the origin can differ from 0, 0.



回答2:

Try to set in your custom uiview's xib file set class HZEventDrawerView and change line

eventDrawer = (HZEventDrawerView *)[[NSBundle mainBundle] loadNibNamed:@"HZEventDrawerView" owner:self options:nil][0];

On:

eventDrawer = [[HZEventDrawerView alloc] initWithNibNamed:@"HZEventDrawerView" bundle:nil];


回答3:

The following line is the problem:

// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];

You are retrieving a new cell from the UITableViewDataSource! Instead, you should get the cell from the UITableView directly:

// Grab the selected cell and make sure it's an event cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

Cheers!