Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

ios - Display a UIView within a UITableViewCell pragmatically

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;
        }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...