Reputation: 83
I have taken two prototype cells called CategoryTableCell and SubCategoryTableCell in tableview
Initially, I need to show all CategoryTableCell
data in TableView
. Here if I click CategoryTableCell
then I need to show that Category's subcategories in SubCategoryTableCell
. How to do that?
I need to hide all subcategories. If I click any one category from CategoryTableCell
then I need to its subcategories.
Code:
extension CategoryVC: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return self.activeCategories?.count ?? 0 : 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.activeCategories?[section].sub_categories?.count ?? 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "SubCategoryTableCell", for: indexPath) as! SubCategoryTableCell
cell.selectionStyle = .none
let activesubCat = self.activeCategories?[indexPath.section].sub_categories
let indexData = activesubCat?[indexPath.row]
cell.lblTitle?.text = langType == .en ? indexData?.details?.first?.title : indexData?.details?[1].title
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = tableView.dequeueReusableCell(withIdentifier: "CategoryTableCell") as! CategoryTableCell
let indexData = self.activeCategories?[section]
view.btnDetails?.tag = section
view.lblTitle?.text = langType == .en ? indexData?.details?.first?.title : indexData?.details?[1].title
return view
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return isFromFilter ? 40 : 0
}
With the code, I am getting all categories in red colour and subcategories in dark grey colour text. But, here cell is not expanding and collapse. I need to show all red colour text initially. Here if I tap on the cell then it has to expand and show all grey colour text. How to do that? Please do help with the code.
o/p: coming like this with above code
[![screenshot][1]][1]
EDIT
JSON Model:
public class Category_json {
public var id : Int?
public var status : String?
public var sub_categories : Array<Sub_categories>?
var isExpanded = true
required public init?(dictionary: NSDictionary) {
id = dictionary["id"] as? Int
status = dictionary["status"] as? String
if (dictionary["sub_categories"] != nil) { sub_categories = Sub_categories.modelsFromDictionaryArray(array: dictionary["sub_categories"] as? NSArray ?? NSArray()) }
if (dictionary["details"] != nil) { details = Details.modelsFromDictionaryArray(array: dictionary["details"] as? NSArray ?? NSArray()) }
}
public func dictionaryRepresentation() -> NSDictionary {
let dictionary = NSMutableDictionary()
dictionary.setValue(self.id, forKey: "id")
dictionary.setValue(self.status, forKey: "status")
return dictionary
}
}
and public var activeCategories : Array<Category_json>?
EDIT2:
@objc func expandMe() {
print("expand me")
}
Upvotes: 0
Views: 283
Reputation: 100533
Add a property to your activeCategories
array 's model
var isExpanded = true
Then inside numberOfRowsInSection
guard let item = self.activeCategories?[section] else { return 0 }
return item.isExpanded ? (item.sub_categories?.count ?? 0) : 0
Then play with isExpanded
and refresh the table
Edit
Inside viewForHeaderInSection
let header = tableView.dequeueReusableCell(withIdentifier: "CategoryTableCell") as! CategoryTableCell
button.tag = section
Then inside action
@objc headerClicked(_ sender:UIButton) {
let section = sender.tag
guard let item = self.activeCategories?[section] else { return 0 }
item.isExpanded.toggle()
tableView.reloadData()
}
Upvotes: 1