务必在UITableViewCell中显示一个UIView
我使用下面的代码从XIB
实例化UIView
,然后将其添加到选定的UITableViewCell
进行显示。 当我运行代码并触摸单元格时,我可以遍历代码并看到所有内容都正确实例化(没有任何内容为零),但视图从不显示。
我有一个UIView上有几个button。 在界面生成器中,我将UIView设置为使用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; } } }
更新
为了testing,看看是否是我的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; }
而不是eventDrawer.bounds = eventCell.bounds;
尝试eventDrawer.frame = eventCell.bounds;
视图的bounds
是从内部看的,所以原点将是0,0。然而,视图的frame
是相对于它的超视图的,所以原点可以不同于0,0。
尝试在自定义uiview的xib文件集类HZEventDrawerView
设置并更改行
eventDrawer = (HZEventDrawerView *)[[NSBundle mainBundle] loadNibNamed:@"HZEventDrawerView" owner:self options:nil][0];
上:
eventDrawer = [[HZEventDrawerView alloc] initWithNibNamed:@"HZEventDrawerView" bundle:nil];
以下行是问题:
// 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];
干杯!