务实地在 UITableViewCell 中显示 UIView [英] Display a UIView within a UITableViewCell pragmatically

查看:23
本文介绍了务实地在 UITableViewCell 中显示 UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码从 XIB 实例一个 UIView,然后将其添加到选定的 UITableViewCell 以进行显示.当我运行代码并触摸单元格时,我可以遍历代码并看到所有内容都正确实例化(没有任何内容为零),但从未显示视图.

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.

我有一个带有几个按钮的 UIView.在 Interface Builder 中,我将 UIView 设置为使用 UIView 的子类,除了由 Xcode 生成的样板之外,该子类目前没有任何代码.我希望有人能指出我在使用此代码使其工作时犯的任何明显错误.

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.

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

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

更新

为了测试并查看是否是我的 XIB 文件或子类导致了问题,我停止使用它,实例化了一个 UIView,向其中添加了一个 UIButton,然后将其添加到 Cell.contentView 子视图中.当我触摸电池时,什么也没有发生.

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

推荐答案

以下是问题所在:

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

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

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

干杯!

这篇关于务实地在 UITableViewCell 中显示 UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆