I have two sections the first section is assessments list and second section companies and users. I am trying to expand the 2nd section with users’ data but my problem is user data expanded by default. I use this blog post as a reference https://neilhiddink.com/tutorials-uitableview-with-expandable-cells/
import UIKit
import Device
class HubTableViewController: UITableViewController {
var assessments:[Assessment]?
var companies:[Company]?
override func viewDidLoad() {
super.viewDidLoad()
assessments = [Assessment(Id: 1, assessmentName: "ASRS (2-5 Years)", assessmenType: "Full-length Parent (English)", balance: 250),
Assessment(Id: 2, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 3, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 4, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 5, assessmentName: "assessmentName", assessmenType: "ShassessmentName", balance: 250),
Assessment(Id: 6, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 7, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id:8, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 9, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250),
Assessment(Id: 10, assessmentName: "assessmentName", assessmenType: "assessmentName", balance: 250)]
companies = [Company(Id: 1, companyName: "Disney", numberofUser: 3,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 2, companyName: "Facebook", numberofUser: 4,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 3, companyName: "Facebook", numberofUser: 2,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 4, companyName: "Disney", numberofUser: 3,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 5, companyName: "Facebook", numberofUser: 4,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 7, companyName: "Facebook", numberofUser: 2,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 8, companyName: "Disney", numberofUser: 3,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 9, companyName: "Facebook", numberofUser: 4,isExpand: false,users:["User1","User1","User1","User1"]),
Company(Id: 10, companyName: "Facebook", numberofUser: 2,isExpand: false,users:["User1","User1","User1","User1"])]
tableView.register(HubCustomHeader.self,
forHeaderFooterViewReuseIdentifier: "sectionHeader")
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch Device.size() {
case .screen4_7Inch:
if section == 0{
return 3
}
if section == 1 {
if (companies?[section ].isExpand == true)
{
return (companies?[section ].users.count)!
}
else {
return 3
}
}
else {
return 3
}
default:
if section == 0{
return 3
}
else {
return 3
}
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.row == 0 {
if companies![indexPath.section ].isExpand == true {
companies![indexPath.section].isExpand = false
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
} else {
companies![indexPath.section].isExpand = true
let sections = IndexSet.init(integer: indexPath.section)
tableView.reloadSections(sections, with: .none)
}
} else {
companies?[indexPath.section].isExpand = false
let sections = IndexSet.init(integer: indexPath.section )
tableView.reloadSections(sections, with: .none)
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0{
let cell = tableView.dequeueReusableCell(withIdentifier: "assessmentCell", for: indexPath) as! AssessmentsTableViewCell
if let a = assessments?[indexPath.row]{
cell.lblAssessmentName.text = a.assessmentName
cell.lblAssessmentsType.text = a.assessmenType
cell.lblBalance.text = "\(a.balance)"
}
return cell
}
else {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCell(withIdentifier: "companyCell", for: indexPath) as! CompanyTableViewCell
cell.setlblCompanyNameText(companyName: (companies?[indexPath.row].companyName)!)
cell.setlblNumberOfUsersText(numberOfUsers: "\((companies?[indexPath.row].numberofUser)!)")
return cell
default:
let cell = tableView.dequeueReusableCell(withIdentifier: "userCell", for: indexPath) as! CompanyTableViewCell
print(companies![indexPath.section].users[indexPath.row].count)
if let company = companies?[indexPath.section]{
//cell.setlblUsernameText(username: company.users[indexPath.row-1])
cell.lblUsername.text = company.users[indexPath.row]
}
return cell
}
}
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if (section == 0){
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:
"sectionHeader") as! HubCustomHeader
view.title.text = "My Balances"
view.button.setTitle("VIEW ALL", for: .normal)
return view
}
else {
let view = tableView.dequeueReusableHeaderFooterView(withIdentifier:
"sectionHeader") as! HubCustomHeader
view.title.text = "My Customers"
view.button.setTitle("ADD", for: .normal)
return view
}
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0{
return 50
}
else {
return 50
}
}
}
extension HubTableViewController{
struct Assessment {
var Id:Int
var assessmentName:String
var assessmenType:String
var balance:Int
}
struct Company {
var Id:Int
var companyName:String
var numberofUser:Int
var isExpand:Bool
var users:[String]
}
}
**