自定义 UIButton TableView 没有响应
Custom UIButton TableView not responding
我制作了一个自定义 UIButton,其中有一个折叠 UITableView。点击按钮后,table 视图高度约束会发生变化,并且 table 视图会打开并带有折叠效果。
我设法为提供 table 数据的按钮创建委托,但我无法获取 didSelectRow 并滚动 tableView。我认为这可能与 tableView 折叠时需要放大按钮框架有关,但我没能做到。
这是自定义 UIButton:
CollapseButton.swift
Created by Matan Levi on 21/12/2020.
import UIKit
protocol CollapseButtonDelegate
{
var options : [String] { get }
func didSelectRow(tableView: UITableView, indexPath: IndexPath)
}
@IBDesignable class CollapseButton: UIButton {
@IBOutlet weak var btnTableViewOutlet: UITableView!
@IBOutlet weak var btnCollapseHeight: NSLayoutConstraint!
let open = CGFloat(113)
let close = CGFloat.zero
var isOpen = false
var delegate : CollapseButtonDelegate?
// init used if the view is created programmatically
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
// init used if the view is created through IB
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
override func awakeAfter(using aDecoder: NSCoder) -> Any? {
guard subviews.isEmpty else { return self }
let view = UINib(nibName: "CollapseButton", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
// Do custom initialization here
private func customInit()
{
}
@IBAction func collapseBtn(_ sender: UIButton) {
self.btnTableViewOutlet.addBorder()
UIView.animate(withDuration: 1) {
self.btnCollapseHeight.constant = self.isOpen ? self.close : self.open
sender.imageView?.transform = self.isOpen ? CGAffineTransform.identity : CGAffineTransform(rotationAngle: .pi)
self.layoutIfNeeded()
}
isOpen = !isOpen
}
}
extension CollapseButton : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return delegate?.options.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = delegate?.options[indexPath.row]
cell.backgroundColor = .green
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didSelectRow(tableView: tableView, indexPath: indexPath)
}
}
下面是按钮实现:
extension PortfolioViewController : CollapseButtonDelegate
{
func didOpenCollapse() {
accountBtnOutlet.setNeedsUpdateConstraints()
accountBtnOutlet.addBorder()
}
var options: [String] {
return FakeDataSupplier.shared.getAccountsData()
}
func didSelectRow(tableView: UITableView, indexPath: IndexPath) {
print(indexPath.row)
}
}
我试过了:
1.Checking 已启用用户交互
2.Checking 代表和数据源连接
3.Checking ui 调试器
中的 UITableView 之上没有任何东西
如有任何建议,我们将不胜感激
编辑
添加了屏幕截图:
所以实际的解决方案是这样的:
起初我将我的 class 更改为 UIView,这使得在我的情况下添加更多视图或 table 方面的事情变得更加方便。之后我为 table 的打开做了委托,在 viewController 中使用按钮我为按钮做了一个高度限制出口并在该按钮打开折叠时更新它 table .
感谢@DonMag 指导我研究解决方案。
我会尽量让它更简单易用,但在那之前,
这是半决赛 buttonView class:
//
// CollapseView.swift
// IBITrade
//
// Created by Matan Levi IBI on 22/12/2020.
//
import UIKit
protocol CollapseViewDelegate
{
func setTableViewOptions(collapseView : CollapseView) -> [String]
func didSelectRow(collapseView : CollapseView, tableView: UITableView, indexPath: IndexPath)
func isCollapseOpen(collapseView : CollapseView, isOpen : Bool)
}
@IBDesignable class CollapseView: UIView {
@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var collapseBtnOutlet: UIButton!
@IBOutlet weak var collapseTableViewOutlet: UITableView!
public var buttonTitle: String = "" {
didSet {
self.collapseBtnOutlet.setTitle(buttonTitle, for: .normal)
}
}
var delegate : CollapseViewDelegate?
let open = CGFloat(113)
let close = CGFloat.zero
var isOpen = false
// init used if the view is created programmatically
let nibName = "CollapseView"
var contentView:UIView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
@IBAction func openCloseTable(_ sender: UIButton) {
collapseTableViewOutlet.addBorder()
UIView.animate(withDuration: 1) { [self] in
self.tableViewHeightConstraint.constant = self.isOpen ? self.close : self.open
sender.imageView?.transform = self.isOpen ? CGAffineTransform.identity : CGAffineTransform(rotationAngle: .pi)
delegate?.isCollapseOpen(collapseView: self, isOpen: isOpen)
self.layoutIfNeeded()
}
isOpen = !isOpen
}
}
extension CollapseView : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return delegate?.setTableViewOptions(collapseView: self).count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = delegate?.setTableViewOptions(collapseView: self)[indexPath.row]
cell.backgroundColor = .green
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didSelectRow(collapseView: self, tableView: tableView, indexPath: indexPath)
}
}
我制作了一个自定义 UIButton,其中有一个折叠 UITableView。点击按钮后,table 视图高度约束会发生变化,并且 table 视图会打开并带有折叠效果。 我设法为提供 table 数据的按钮创建委托,但我无法获取 didSelectRow 并滚动 tableView。我认为这可能与 tableView 折叠时需要放大按钮框架有关,但我没能做到。
这是自定义 UIButton:
CollapseButton.swift
Created by Matan Levi on 21/12/2020.
import UIKit
protocol CollapseButtonDelegate
{
var options : [String] { get }
func didSelectRow(tableView: UITableView, indexPath: IndexPath)
}
@IBDesignable class CollapseButton: UIButton {
@IBOutlet weak var btnTableViewOutlet: UITableView!
@IBOutlet weak var btnCollapseHeight: NSLayoutConstraint!
let open = CGFloat(113)
let close = CGFloat.zero
var isOpen = false
var delegate : CollapseButtonDelegate?
// init used if the view is created programmatically
override init(frame: CGRect) {
super.init(frame: frame)
self.customInit()
}
// init used if the view is created through IB
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.customInit()
}
override func awakeAfter(using aDecoder: NSCoder) -> Any? {
guard subviews.isEmpty else { return self }
let view = UINib(nibName: "CollapseButton", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
view.translatesAutoresizingMaskIntoConstraints = false
return view
}
// Do custom initialization here
private func customInit()
{
}
@IBAction func collapseBtn(_ sender: UIButton) {
self.btnTableViewOutlet.addBorder()
UIView.animate(withDuration: 1) {
self.btnCollapseHeight.constant = self.isOpen ? self.close : self.open
sender.imageView?.transform = self.isOpen ? CGAffineTransform.identity : CGAffineTransform(rotationAngle: .pi)
self.layoutIfNeeded()
}
isOpen = !isOpen
}
}
extension CollapseButton : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return delegate?.options.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = delegate?.options[indexPath.row]
cell.backgroundColor = .green
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didSelectRow(tableView: tableView, indexPath: indexPath)
}
}
下面是按钮实现:
extension PortfolioViewController : CollapseButtonDelegate
{
func didOpenCollapse() {
accountBtnOutlet.setNeedsUpdateConstraints()
accountBtnOutlet.addBorder()
}
var options: [String] {
return FakeDataSupplier.shared.getAccountsData()
}
func didSelectRow(tableView: UITableView, indexPath: IndexPath) {
print(indexPath.row)
}
}
我试过了:
1.Checking 已启用用户交互 2.Checking 代表和数据源连接 3.Checking ui 调试器
中的 UITableView 之上没有任何东西如有任何建议,我们将不胜感激
编辑
添加了屏幕截图:
所以实际的解决方案是这样的: 起初我将我的 class 更改为 UIView,这使得在我的情况下添加更多视图或 table 方面的事情变得更加方便。之后我为 table 的打开做了委托,在 viewController 中使用按钮我为按钮做了一个高度限制出口并在该按钮打开折叠时更新它 table . 感谢@DonMag 指导我研究解决方案。
我会尽量让它更简单易用,但在那之前, 这是半决赛 buttonView class:
//
// CollapseView.swift
// IBITrade
//
// Created by Matan Levi IBI on 22/12/2020.
//
import UIKit
protocol CollapseViewDelegate
{
func setTableViewOptions(collapseView : CollapseView) -> [String]
func didSelectRow(collapseView : CollapseView, tableView: UITableView, indexPath: IndexPath)
func isCollapseOpen(collapseView : CollapseView, isOpen : Bool)
}
@IBDesignable class CollapseView: UIView {
@IBOutlet weak var tableViewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var collapseBtnOutlet: UIButton!
@IBOutlet weak var collapseTableViewOutlet: UITableView!
public var buttonTitle: String = "" {
didSet {
self.collapseBtnOutlet.setTitle(buttonTitle, for: .normal)
}
}
var delegate : CollapseViewDelegate?
let open = CGFloat(113)
let close = CGFloat.zero
var isOpen = false
// init used if the view is created programmatically
let nibName = "CollapseView"
var contentView:UIView?
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
guard let view = loadViewFromNib() else { return }
view.frame = self.bounds
self.addSubview(view)
contentView = view
}
func loadViewFromNib() -> UIView? {
let bundle = Bundle(for: type(of: self))
let nib = UINib(nibName: nibName, bundle: bundle)
return nib.instantiate(withOwner: self, options: nil).first as? UIView
}
@IBAction func openCloseTable(_ sender: UIButton) {
collapseTableViewOutlet.addBorder()
UIView.animate(withDuration: 1) { [self] in
self.tableViewHeightConstraint.constant = self.isOpen ? self.close : self.open
sender.imageView?.transform = self.isOpen ? CGAffineTransform.identity : CGAffineTransform(rotationAngle: .pi)
delegate?.isCollapseOpen(collapseView: self, isOpen: isOpen)
self.layoutIfNeeded()
}
isOpen = !isOpen
}
}
extension CollapseView : UITableViewDelegate, UITableViewDataSource
{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return delegate?.setTableViewOptions(collapseView: self).count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = delegate?.setTableViewOptions(collapseView: self)[indexPath.row]
cell.backgroundColor = .green
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
delegate?.didSelectRow(collapseView: self, tableView: tableView, indexPath: indexPath)
}
}