【发布时间】:2018-05-02 13:26:11
【问题描述】:
我正在实现一个带有部分和可折叠标题的UITableView。该部分的单元格有标签和按钮。标签已折叠,但当我折叠标题时按钮保持空闲(未隐藏)。
我需要一个实现,其中按钮在单元格展开时出现,在单元格折叠时隐藏。我使用了一个自定义单元格,该单元格在堆栈视图中具有按钮。在this picture 中,箭头代表表格部分标题视图,单选按钮折叠后必须隐藏。
代码如下:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (sections[indexPath.section].expanded)
{
return 244
}
else
{
return 0
}
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = ExpandableDashboardView()
print("title\(sections[section].title)")
header.customInit(title: sections[section].title, section: section, delegate: self,isOpen:sections[section].expanded)
return header
}
func toggleSection(header: ExpandableDashboardView, section: Int) {
sections[section].expanded = !sections[section].expanded
settingsTable.beginUpdates()
if sections[section].expanded == true
{
header.changeInit(title: sections[section].title, section: section, delegate: self)
}
else
{
header.customInit(title: sections[section].title, section: section, delegate: self, isOpen: sections[section].expanded)
}
class ExpandableDashboardView: UITableViewHeaderFooterView{
var delegate : ExpandableDashboardDelegate!
var section:Int!
var image = UIImage()
override init(reuseIdentifier: String?) {
super.init(reuseIdentifier: reuseIdentifier)
self.addGestureRecognizer(UITapGestureRecognizer(target:self,action:#selector(selectDashboardHeader)))
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func customInit(title:String,section:Int,delegate:ExpandableDashboardDelegate,isOpen:Bool)
{
// if isOpen == true{
//// self.textLabel?.text = "\u{25B6} \(title)"
// self.textLabel?.text = "\u{25BC} \(title)"
// }
// else{
// self.textLabel?.text = "\u{25BC} \(title)"
// }
// self.textLabel?.textColor = UIColor.black
self.textLabel?.text = "\u{25B6} \(title)" //02C3
self.section = section
self.delegate = delegate
layoutSubviews()
}
func changeInit(title:String,section:Int,delegate:ExpandableDashboardDelegate)
{
self.textLabel?.textColor = UIColor.black
self.textLabel?.text = "\u{25BC} \(title)"
self.section = section
self.delegate = delegate
layoutSubviews()
}
func selectDashboardHeader(gestureRecognizer:UIGestureRecognizer)
{
let cell = gestureRecognizer.view as! ExpandableDashboardView
delegate.toggleSection(header: self , section: cell.section)
print("section\(cell.section)")
}
override func layoutSubviews() {
super.layoutSubviews()
self.textLabel?.textColor = UIColor.init(red: 17.0/255.0, green: 83.0/255.0, blue: 168.0/255.0, alpha: 1.0)
self.contentView.backgroundColor = UIColor.init(red: 255.0/255.0, green: 255.0/255.0, blue: 255.0/255.0, alpha: 1.0)
}
【问题讨论】:
-
没有任何代码很难提供帮助,请尝试添加一些 example code 来说明您的问题。
-
我已经附上了...