我创建带有标题的扩展表视图。 在这里,我遇到一个问题,即我没有获取用于扩展表格单元格的表格单元格索引路径。
@IBOutlet weak var myTableView: UITableView!
var hiddenSections: [Int] = []
我点击了标题单元格的按钮
@IBOutlet weak var tapBtn: UIButton!
在我的视图控制器中
public func numberOfSections(in tableView: UITableView) -> Int
{
return 5
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 3
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerCell = myTableView.dequeueReusableCell(withIdentifier: "header") as! HeaderCell?
headerCell?.tapBtn.tag = section
headerCell?.tapBtn.addTarget(self, action:#selector(headerPressed(_:)), for:.touchUpInside)
return headerCell
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = keycontactsTableView.dequeueReusableCell(withIdentifier: "expandCell") as! expandCell?
return cell!
}
@objc func headerPressed(_ sender: UIButton)
{
print("Header tapped %@", sender.tag as Any)
let indexPath = IndexPath(row: 0, section: sender.tag)
print(indexPath)
if hiddenSections.contains(sender.tag)
{
hiddenSections.remove(at: hiddenSections.index(of: sender.tag)!)
myTableView.reloadSections(NSIndexSet(index: sender.tag) as IndexSet, with: .automatic)
myTableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.top, animated: true)
}
else
{
hiddenSections.append(sender.tag)
myTableView.reloadSections(NSIndexSet(index: sender.tag) as IndexSet, with: .automatic)
}
}
如何在headerPressed上展开单元格
答案 0 :(得分:0)
故事板
代码
import UIKit
class ExpandCollapseViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var expTblVw: UITableView!
var expandData = [NSMutableDictionary]()
var binType = ["aaa","bbb","ccc","ddd","eee"]
override func viewDidLoad() {
super.viewDidLoad()
for _ in 0..<5
{
self.expandData.append(["isOpen":"1","data":["ONCE A WEEK","TWICE A WEEK","EVERY TWO WEEKS","EVERY THREE WEEKS","ONCE A MONTH"],"isSelect":"No"])
}
// Do any additional setup after loading the view.
}
//MARK: - Tableview Delegates
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.expandData[section].value(forKey: "isOpen") as! String == "1"{
return 0
}else{
let dataarray = self.expandData[section].value(forKey: "data") as! NSArray
return dataarray.count
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return self.expandData.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 45
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:expTblvwCell = tableView.dequeueReusableCell(withIdentifier: "CellID", for: indexPath as IndexPath) as! expTblvwCell
let dataarray = self.expandData[indexPath.section].value(forKey: "data") as! NSArray
cell.lblTitle.text = dataarray[indexPath.row] as? String
let selData = self.expandData[indexPath.section].value(forKey: "isSelect") as! NSString
if selData == "No" {
cell.lblImg.image = UIImage(named: "round2")!
} else {
if selData.intValue == indexPath.row {
cell.lblImg.image = UIImage(named: "round1")!
} else {
cell.lblImg.image = UIImage(named: "round2")!
}
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
{
let headerView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 45))
let label = UILabel(frame: CGRect(x: 5, y: 9, width: headerView.frame.size.width - 50, height: 27))
let img = UIImageView(frame: CGRect(x: headerView.frame.size.width-50, y: 9, width: 27, height: 27))
if self.expandData[section].value(forKey: "isOpen") as! String == "1"{
img.image = UIImage(named: "drop_down_arrow_black")!
} else {
img.image = UIImage(named: "drop_up_arrow_black")!
}
headerView.backgroundColor = .white
headerView.layer.borderWidth = 1.0
headerView.layer.cornerRadius = 5
headerView.layer.borderColor = UIColor.black.cgColor
label.text = (binType[section]).uppercased()
label.textColor = UIColor(red:0/255, green:173/255, blue:250/255, alpha: 1)
headerView.addSubview(label)
headerView.addSubview(img)
headerView.tag = section + 100
// self.tblFrequency.tableHeaderView = headerView.
let tapgesture = UITapGestureRecognizer(target: self , action: #selector(self.sectionTapped(_:)))
headerView.addGestureRecognizer(tapgesture)
return headerView
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.expandData[indexPath.section].setValue(String(indexPath.row), forKeyPath: "isSelect")
self.expTblVw.reloadData()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
self.expandData[indexPath.section].setValue("1", forKey: "isOpen")
self.expTblVw.reloadSections(IndexSet(integer: indexPath.section), with: .automatic)
})
}
func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
let FooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.bounds.size.width, height: 10))
FooterView.backgroundColor = .clear
return FooterView
}
@objc func sectionTapped(_ sender: UITapGestureRecognizer){
if(self.expandData[(sender.view?.tag)! - 100].value(forKey: "isOpen") as! String == "1"){
self.expandData[(sender.view?.tag)! - 100].setValue("0", forKey: "isOpen")
}else{
self.expandData[(sender.view?.tag)! - 100].setValue("1", forKey: "isOpen")
}
self.expTblVw.reloadSections(IndexSet(integer: (sender.view?.tag)! - 100), with: .automatic)
}
}
class expTblvwCell: UITableViewCell {
@IBOutlet weak var lblTitle: UILabel!
@IBOutlet weak var lblImg: UIImageView!
}
结果