Reputation: 55
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>>>
Upvotes: 0
Views: 54