添加到超级视图时,带有表视图崩溃的下拉列表
我想调用 tableview dropdownlist,但它会在 viewcontroller 上崩溃,因为 superview!.addSubview(transparentView) 单击了文本字段 textbegin。我怎样才能以最好的方式将下拉列表与表格视图一起使用。请帮助下拉菜单
我想从 viewController 调用函数
private var dropdownMenu: RegistrationDropdownMenu = RegistrationDropdownMenu(identifier: RegStepTwoIndentifier.regStepTwoTable)
RegistrationDropdownMenu().openDropdownMenu(inputField: inputField)
class RegistrationDropdownMenu: UITableView {
private var identifier: String
private var inputField: InputField?
private var data: [RegistrationAPIResponse.RegistartionItems] = []
private var transparentView = UIView()
private var maxHeight: CGFloat = 300
public var isDropdownMenuOpened: Bool = false
init(identifier: String) {
self.identifier = identifier
super.init(frame: .zero, style: .plain)
configDropdownMenu()
}
private func configDropdownMenu() {
self.delegate = self
self.dataSource = self
self.translatesAutoresizingMaskIntoConstraints = false
self.register(RegistrationCell.self, forCellReuseIdentifier: identifier)
self.allowsSelection = true
self.separatorStyle = .none
self.layer.masksToBounds = true
self.backgroundColor = Styles.dropDownMenuBackgroundColor
self.layer.borderColor = Styles.borderColor.cgColor
self.layer.borderWidth = Styles.borderWidth
self.rowHeight = UITableView.automaticDimension
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension RegistrationDropdownMenu {
public func openDropdownMenu(inputField: inputField) {
isDropdownMenuOpened = true
self.inputField = inputField
data = RegistrationDataRepository().get(for: .birthdayMonth)
self.layoutIfNeeded()
guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
let frames = inputField.convert(inputField.bounds, to: keyWindow)
superview!.addSubview(transparentView)
superview!.addSubview(self)
self.reloadData()
self.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
self.frame = CGRect(
x: frames.origin.x,
y: frames.origin.y + frames.height - 1,
width: frames.width,
height: maxHeight
)
}
public func closeDropDownMenu() {
self.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
isDropdownMenuOpened = false
self.data = []
guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
let frames = inputField!.convert(inputField!.bounds, to: keyWindow)
self.transparentView.alpha = 0
self.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height, width: frames.width, height: 0)
}
@objc private func backgroundBehavior() {
closeDropDownMenu()
}
}
extension RegistrationDropdownMenu: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
cell.textLabel?.text = data[indexPath.row].label
cell.textLabel?.font = UIFont.systemFont(ofSize: Styles.dropdownMenuTextSize, weight: .regular)
cell.textLabel?.textColor = Styles.dropDownMenuTextColor
cell.textLabel?.numberOfLines = 3
cell.textLabel?.lineBreakMode = .byWordWrapping
return cell
}
func tableView(_ tableview: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return Styles.dropdownMenuRowSize
}
func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {
inputField!.text = data[indexPath.row].label
closeDropDownMenu()
}
}
class RegistrationCell: UITableViewCell {}
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800; baseClass = UITableView; frame = (20 312; 335 300); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000f971b0>; layer = <CALayer: 0x600001c6e700>; contentOffset: {0, 0}; contentSize: {335, 572}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800; baseClass = UITableView; frame = (20 312; 335 300); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000f971b0>; layer = <CALayer: 0x600001c6e700>; contentOffset: {0, 0}; contentSize: {335, 572}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800>>>
I would like to call tableview dropdownlist, but it will crash on viewcontroller because of superview!.addSubview(transparentView) clicked on textfield textbegin. How can i use the dropdownlist with tableview in the best way. please help with the dropdownmenu
I would like to call function from viewController
private var dropdownMenu: RegistrationDropdownMenu = RegistrationDropdownMenu(identifier: RegStepTwoIndentifier.regStepTwoTable)
RegistrationDropdownMenu().openDropdownMenu(inputField: inputField)
class RegistrationDropdownMenu: UITableView {
private var identifier: String
private var inputField: InputField?
private var data: [RegistrationAPIResponse.RegistartionItems] = []
private var transparentView = UIView()
private var maxHeight: CGFloat = 300
public var isDropdownMenuOpened: Bool = false
init(identifier: String) {
self.identifier = identifier
super.init(frame: .zero, style: .plain)
configDropdownMenu()
}
private func configDropdownMenu() {
self.delegate = self
self.dataSource = self
self.translatesAutoresizingMaskIntoConstraints = false
self.register(RegistrationCell.self, forCellReuseIdentifier: identifier)
self.allowsSelection = true
self.separatorStyle = .none
self.layer.masksToBounds = true
self.backgroundColor = Styles.dropDownMenuBackgroundColor
self.layer.borderColor = Styles.borderColor.cgColor
self.layer.borderWidth = Styles.borderWidth
self.rowHeight = UITableView.automaticDimension
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
extension RegistrationDropdownMenu {
public func openDropdownMenu(inputField: inputField) {
isDropdownMenuOpened = true
self.inputField = inputField
data = RegistrationDataRepository().get(for: .birthdayMonth)
self.layoutIfNeeded()
guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
let frames = inputField.convert(inputField.bounds, to: keyWindow)
superview!.addSubview(transparentView)
superview!.addSubview(self)
self.reloadData()
self.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
self.frame = CGRect(
x: frames.origin.x,
y: frames.origin.y + frames.height - 1,
width: frames.width,
height: maxHeight
)
}
public func closeDropDownMenu() {
self.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: false)
isDropdownMenuOpened = false
self.data = []
guard let keyWindow = UIApplication.shared.windows.first(where: { $0.isKeyWindow }) else { return }
let frames = inputField!.convert(inputField!.bounds, to: keyWindow)
self.transparentView.alpha = 0
self.frame = CGRect(x: frames.origin.x, y: frames.origin.y + frames.height, width: frames.width, height: 0)
}
@objc private func backgroundBehavior() {
closeDropDownMenu()
}
}
extension RegistrationDropdownMenu: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableview: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableview: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableview.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
cell.textLabel?.text = data[indexPath.row].label
cell.textLabel?.font = UIFont.systemFont(ofSize: Styles.dropdownMenuTextSize, weight: .regular)
cell.textLabel?.textColor = Styles.dropDownMenuTextColor
cell.textLabel?.numberOfLines = 3
cell.textLabel?.lineBreakMode = .byWordWrapping
return cell
}
func tableView(_ tableview: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return Styles.dropdownMenuRowSize
}
func tableView(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath) {
inputField!.text = data[indexPath.row].label
closeDropDownMenu()
}
}
class RegistrationCell: UITableViewCell {}
[TableView] Warning once only: UITableView was told to layout its visible cells and other contents without being in the view hierarchy (the table view or one of its superviews has not been added to a window). This may cause bugs by forcing views inside the table view to load and perform layout without accurate information (e.g. table view bounds, trait collection, layout margins, safe area insets, etc), and will also cause unnecessary performance overhead due to extra layout passes. Make a symbolic breakpoint at UITableViewAlertForLayoutOutsideViewHierarchy to catch this in the debugger and see what caused this to occur, so you can avoid this action altogether if possible, or defer it until the table view has been added to a window. Table view: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800; baseClass = UITableView; frame = (20 312; 335 300); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000f971b0>; layer = <CALayer: 0x600001c6e700>; contentOffset: {0, 0}; contentSize: {335, 572}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800; baseClass = UITableView; frame = (20 312; 335 300); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000f971b0>; layer = <CALayer: 0x600001c6e700>; contentOffset: {0, 0}; contentSize: {335, 572}; adjustedContentInset: {0, 0, 0, 0}; dataSource: <Hong_Kong_International_Airport.RegistrationDropdownMenu: 0x7ff007190800>>>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论