以实用方式在UITableViewCell中显示UIView

时间:2013-08-18 06:09:09

标签: ios objective-c uiview uitableview

我使用以下代码从UIView实例XIB,然后将其添加到选定的UITableViewCell进行显示。当我运行代码并触摸单元格时,我可以浏览代码并看到所有内容都已正确实例(没有任何内容),但视图永远不会显示。

我有一个带有几个按钮的UIView。在Interface Builder中,我将UIView设置为使用UIView的子类,该子类目前没有任何代码,除了Xcode生成的样板。我希望有人可以指出我在使用此代码时发生的任何明显错误。

请注意,有一次我在UITalbeViewCell中显示了UIView,但是在重构过程中我搞砸了一些东西,最后重新编写了代码来处理这个问题。当发生这种情况时,我再也无法在单元格中显示UIView。

@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;
        }
    }
}

更新

为了测试并查看是否是我的XIB文件或导致问题的子类,我停止使用它,实例化UIView,向其添加了一个UIButton,然后将其添加到Cell.contentView子视图中。当我触摸细胞时,没有任何反应。

// 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;
        }

3 个答案:

答案 0 :(得分:0)

而不是eventDrawer.bounds = eventCell.bounds;尝试eventDrawer.frame = eventCell.bounds;

从内部查看视图的bounds,因此原点将为0,0。但是视图的frame是相对于它的超级视图,因此原点可以与0不同, 0

答案 1 :(得分:0)

尝试设置自定义uiview的xib文件集类HZEventDrawerView并更改行

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

在:

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

答案 2 :(得分:0)

以下问题是:

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

您正在从UITableViewDataSource检索新单元格! 相反,您应该直接从UITableView获取单元格:

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

干杯!