问题描述
我已经接受了 two prototype cells called CategoryTableCell and SubCategoryTableCell in tableview
最初,我需要在 CategoryTableCell
中显示所有 TableView
数据。在这里,如果我点击 CategoryTableCell
,那么我需要在 SubCategoryTableCell
中显示该类别的子类别。如何做到这一点?
我需要隐藏所有子类别。如果我点击 CategoryTableCell
中的任何一个类别,那么我需要它的子类别。
代码:
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
}
通过代码,我获得了红色的所有类别和深灰色文本的子类别。但是,这里的单元格没有膨胀和崩溃。我需要最初显示所有红色文本。在这里,如果我点击单元格,则它必须展开并显示所有灰色文本。怎么做?请帮忙提供代码。
o/p:像上面的代码一样
[![截图][1]][1]
编辑
JSON 模型:
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
}
}
和public var activeCategories : Array<Category_json>?
EDIT2:
@objc func expandMe() {
print("expand me")
}
解决方法
向 activeCategories
数组的模型添加属性
var isExpanded = true
然后在numberOfRowsInSection
内
guard let item = self.activeCategories?[section] else { return 0 }
return item.isExpanded ? (item.sub_categories?.count ?? 0) : 0
然后玩isExpanded
并刷新表格
编辑
内部viewForHeaderInSection
let header = tableView.dequeueReusableCell(withIdentifier: "CategoryTableCell") as! CategoryTableCell
button.tag = section
然后内部动作
@objc headerClicked(_ sender:UIButton) {
let section = sender.tag
guard let item = self.activeCategories?[section] else { return 0 }
item.isExpanded.toggle()
tableView.reloadData()
}