展开/折叠section tableview swift



我有3节及其相应的单元格如何使其扩展/折叠?我已经创建了一个扩展的结构体:Bool值下面是我的代码。*这是我的cellForRowAt *

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch (indexPath.section) {
case 0:
if let cell1 = tableview.dequeueReusableCell(withIdentifier: "TimeCardDetailedTableViewCell", for: indexPath) as? TimeCardDetailedTableViewCell{

return cell1
}
case 1:
if let cell2 = tableview.dequeueReusableCell(withIdentifier: "MessageDetailedTableViewCell", for: indexPath) as? MessageDetailedTableViewCell{


return cell2
}
case 2:
if let cell3 = tableview.dequeueReusableCell(withIdentifier: "CrewDetailedTableViewCell", for: indexPath) as? CrewDetailedTableViewCell{

return cell3
}
default:
return UITableViewCell()
}
return UITableViewCell()
}

——>>这是numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch (section) {
case 0:
return self.timecards.count
case 1:
return self.msglog.count
default:
return self.profile.count
}
}

,——在祝辞的在viewForHeaderInSection

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0{
let header1 = Bundle.main.loadNibNamed("TimeCardHeaderTableViewCell", owner: self, options: nil)?.last as! TimeCardHeaderTableViewCell
header1.backgroundColor = .red
return header1
} else if section == 1 {
let header2 = Bundle.main.loadNibNamed("MessageTableViewCell", owner: self, options: nil)?.last as! MessageTableViewCell
return header2
} else if section == 2 {
let header3 = Bundle.main.loadNibNamed("CrewTableViewCell", owner: self, options: nil)?.last as! CrewTableViewCell
return header3
}
return UITableViewCell()

}

如何在section上实现展开。我是iOS新手

一种方法是像这样更新您的numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
switch (section) { 
case 0: return self.hiddenSections.contains(0) ? 0 : self.timecards.count 
case 1: return self.hiddenSections.contains(1) ? 0 : return self.msglog.count 
case 2: return self.hiddenSections.contains(2) ? 0 : return self.profile.count 
} 
}

viewForHeaderInSection将是这样的,其中您将设置tagselector

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let sectionButton = UIButton()
sectionButton.setTitle(String(section),
for: .normal)
sectionButton.backgroundColor = .systemBlue
sectionButton.tag = section
sectionButton.addTarget(self,
action: #selector(self.hideSection(sender:)),
for: .touchUpInside)
return sectionButton
}

hideSection方法将有逻辑隐藏/显示您的部分

@objc
private func hideSection(sender: UIButton) {
let section = sender.tag

func indexPathsForSection() -> [IndexPath] {
var indexPaths = [IndexPath]()

for row in 0..<self.tableViewData[section].count {
indexPaths.append(IndexPath(row: row,
section: section))
}

return indexPaths
}

if self.hiddenSections.contains(section) {
self.hiddenSections.remove(section)
self.tableView.insertRows(at: indexPathsForSection(),
with: .fade)
} else {
self.hiddenSections.insert(section)
self.tableView.deleteRows(at: indexPathsForSection(),
with: .fade)
}
}

相关内容

  • 没有找到相关文章

最新更新