0

我有一个带有一堆行的 UITableView。当用户点击某一行时,表格顶部会出现一个自定义弹出窗口(即自定义 UIView):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PopUp *myPopUp = [[PopUp alloc] initWithFrame:CGRectMake(0, 0, 320, 568)];
    [self.view addSubview:myPopUp];
}

我正在从笔尖加载我的自定义 UIView PopUp:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadNib];
    }
    return self;
}

- (void) loadNib
{
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"PopUp" owner:self options:nil];
    UIView *mainView = [subviewArray objectAtIndex:0];
    [self addSubview:mainView];
}

在 PopUp 中,按下时有一个按钮会导致 PopUp 关闭:

- (IBAction)closePopUp:(id)sender
{
    [self removeFromSuperview];
} 

按下按钮时弹出窗口消失。但是,下面的 UITableView 不能再进行交互(即用户不能滚动表格,不能点击另一行等)。我希望 PopUp 消失并让表格再次完全交互。谁能解释为什么会发生这种情况以及我如何解决这个问题?谢谢!


用截图编辑

  1. UITableView with a row of data: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#0

  2. When a row is selected, myPopUp appears on top: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#1

  3. When the "x" custom button is pressed, it calls closePopUp, which removes myPopUp from the superview: http://imgur.com/PlIufHI,xGKxUul,qkt27oZ#2

  4. User is unable to interact with the table now. User cannot select a row, scroll through the table, etc.

4

2 回答 2

2

I don't know what's going on in your specific case, but I can tell you that adding subviews to a UITableView may lead to unexpected behavior like this and it's generally a bad idea.

In order to fix what's happening and get a cleaner structure, I would suggest you to add the popup view to the window, rather than to the table view.

[self.view.window addSubview:myPopUp];
于 2013-04-07T22:58:04.243 回答
2

You are actually removing the view that you loaded from the nib file, but the parent is another blank UIView that is capturing every touch within the (0, 0, 320, 568) rect.

Try removing the superview from the closePopUp method:

[self.superview removeFromSuperview];
于 2013-04-07T23:17:23.960 回答