gpt4 book ai didi

ios - 当我将 UITableView 添加到 subview 时,单元格不可选择

转载 作者:行者123 更新时间:2023-11-29 01:54:27 24 4
gpt4 key购买 nike

我有一个自定义类,它是UIView子类。我添加了一个 UITextField 和一个 tableview 作为 subview。然后,在 storyboard 中,我将 UIView 的 类设置为自定义类。

我在自定义类中添加的 tableview 比初始的 UIView 扩展得更大(来自 storyboard)。所以我改变了 self.frame 的高度。

当我运行该应用程序时,我可以在我添加的 textField 中键入内容,但我无法在 tableview 中选择任何内容。我可以看到 单元格,但无法选择它们。

如何使单元格可选择?

这是最相关的代码:

_textField = [[UITextField alloc] initWithFrame:self.bounds];
[self addSubview:_textField];

CGRect tableViewsFrame = self.textField.bounds;
tableViewsFrame.origin.y += self.textField.bounds.size.height;
tableViewsFrame.size.height = 200;

_tableView = [[UITableView alloc] initWithFrame:tableViewsFrame style:UITableViewStylePlain];
[self addSubview:_tableView];

CGRect viewsFrame = self.bounds;
viewsFrame.size.height = self.textField.frame.size.height + self.tableView.frame.size.height + 100;
[self setFrame:viewsFrame];

这是 Jumpshare 上测试项目的链接:testProject

最佳答案

您不小心将 TableViewSelection 更改为 No Selection

enter image description here

编辑:

您可以将 tableView 添加到 window 中,例如:

tableViewsFrame.origin = [self.superview convertPoint:self.frame.origin toView:self.window];
tableViewsFrame.origin.y += self.textField.frame.size.height;
_tableView.frame = tableViewsFrame;
[self.window addSubview:_tableView];

我实现了一个简单的弹出 View :

@interface SimplePopupView : UIControl

+ (void)showView:(UIView *)view fromView:(UIView *)fromView;

@end

@implementation SimplePopupView

+ (void)showView:(UIView *)view fromView:(UIView *)fromView {
UIView *window = [UIApplication sharedApplication].keyWindow;

SimplePopupView *popupView = [[SimplePopupView alloc] initWithFrame:window.bounds];
[popupView addTarget:popupView action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
[window addSubview:popupView];

CGRect viewFrame = fromView.bounds;
viewFrame.origin = [fromView convertPoint:fromView.frame.origin toView:window];
viewFrame.origin.y += fromView.frame.size.height;
viewFrame.size.height = 200;

view.frame = viewFrame;
[popupView addSubview:view];
}

- (void)dismiss {
[self removeFromSuperview];
}

@end

然后你可以:

- (void)setupView {
self.backgroundColor = [UIColor blackColor];
// Configure the TextField
_textField = [[UITextField alloc] initWithFrame:self.bounds];
[_textField setBackgroundColor:[UIColor greenColor]];
[self addSubview:_textField];

// Configure the TableView
_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
_tableView.backgroundColor = [UIColor blueColor];

[SimplePopupView showView:_tableView fromView:_textField];
}

关于ios - 当我将 UITableView 添加到 subview 时,单元格不可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31044186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com