问题描述
我制作了一个自定义 UIButton,其中有一个折叠 UITableView。点击按钮后,表格视图高度约束会发生变化,表格视图会以折叠效果打开。 我设法为提供表格数据的按钮创建委托,但我无法获取 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.检查用户交互已启用 2.检查委托和数据源是否连接 3.在ui调试器中检查UITableView上面没有任何东西
任何建议将不胜感激
编辑
添加截图:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)