I am developing an app which used table view with multiple section and multiple header view. Is it possible to hide randomly section according to server response, i.e.: I have 6 Multiple cells and header view. I got response from the server which show only 2 sections, 4 section and 6 section. I am trying to achieve this, but didn't get success. Here is my code below:
Code:
var allPermissionSection = ["A","B","C","D","E","F"] // default permission
override func numberOfSections(in tableView: UITableView) -> Int {
return totalSectionTable // total count of showing cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
let modelPermission = mainArrayCheckValidations[0] as! MyAcountModel
if section == 0 {
return 1
}
// arraySectionName got from server check index if default section or server reponse index is equal to same then true this continue
if allPermissionSection[0] == arraySectionName[0] {
if arrayTblItems.count > 0 {
return arrayTblItems.count
}else if modelPermission.phoneNumbersEnabled == true {
return 1
}
}
if allPermissionSection[1] == arraySectionName[1] {
if arrayTblItemsForElectronic.count > 0 {
return arrayTblItemsForElectronic.count
}else if modelPermission.econsentEnabled == true {
return 1
}
}
if allPermissionSection[2] == arraySectionName[2] {
if arrayTblItemsForEmergnceContact.count > 0 {
return arrayTblItemsForEmergnceContact.count
}else if modelPermission.emergencyContactsEnabled == true {
return 1
}
}
return 0
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let modelPermission = mainArrayCheckValidations[0] as! MyAcountModel
if indexPath.section == 0 {
return UITableView.automaticDimension
}
if allPermissionSection[0] == arraySectionName[0] {
// if indexPath.section == 1 {
if arrayTblItems.count <= 0 {
if modelPermission.phoneNumbersEnabled == true{
return 40
}else{
return 0
}
}
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if indexPath.section == 2 {
if arrayTblItemsForElectronic.count <= 0 {
if modelPermission.econsentEnabled == true{
return 40
}else{
return 0
}
}
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if indexPath.section == 3 {
if arrayTblItemsForEmergnceContact.count <= 0 {
if modelPermission.emergencyContactsEnabled == true{
return 40
}else{
return 0
}
}
// }
}
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: USER_DETAIL_CELL, for: indexPath) as! UserDetail_Cell
cell.updateWith(mainArrayCheckValidations[indexPath.row], indexPath.row, mainArrayCheckValidations.count, tblCellType)
return cell
}
if allPermissionSection[0] == arraySectionName[0] {
// if indexPath.section == 1 {
if arrayTblItems.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: PHONE_CELL, for: indexPath) as! PhoneNumberDetailCell
cell.updateWith(arrayTblItems[indexPath.row], indexPath.row, arrayTblItems.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
//}
}
if allPermissionSection[1] == arraySectionName[1] {
// if indexPath.section == 2 {
if arrayTblItemsForElectronic.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: CONSTENT_CELL, for: indexPath) as! Constent_Cell
cell.updateWith(arrayTblItemsForElectronic[indexPath.row], indexPath.row, arrayTblItemsForElectronic.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if indexPath.section == 3 {
if arrayTblItemsForEmergnceContact.count > 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: EMERGENCY_CONTACT_CELL, for: indexPath) as! EmergencyCnctCell
cell.updateWith(arrayTblItemsForEmergnceContact[indexPath.row], indexPath.row, arrayTblItemsForEmergnceContact.count, tblCellType)
return cell
}else{
let cell = tableView.dequeueReusableCell(withIdentifier: DEFAULT_CELL, for: indexPath) as! Default_Custom_Cell
cell.lbl_Name.text = ""
return cell
}
// }
}
return UITableViewCell()
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
if section == 0 {
let headerView = setupHeaderView(textHeading: " Details")
return headerView
}
if allPermissionSection[0] == arraySectionName[0] {
//if section == 1 {
let headerView = setupHeaderView(textHeading: "bers")
return headerView
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 2 {
let headerView = setupHeaderView(textHeading: "sent")
return headerView
// }
}
if allPermissionSection[2] == arraySectionName[2] {
// if section == 3 {
let headerView = setupHeaderView(textHeading: "ontacts")
return headerView
// }
}
return UIView()
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
if section == 0 {
return 50
}
if allPermissionSection[0] == arraySectionName[0] {
// if section == 1 {
return 50
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 2 {
return 50
// }
}
if allPermissionSection[1] == arraySectionName[1] {
// if section == 3 {
return 50
// }
}
return 0
}
setupHeaderView(textHeading:String) -> UIView? {
let headerView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: tblVw.frame.width, height: 50))
let headingLbl = UILabel()
let topLbl = UILabel()
let botomLbl = UILabel()
topLbl.translatesAutoresizingMaskIntoConstraints = false
botomLbl.translatesAutoresizingMaskIntoConstraints = false
headerView.addSubview(botomLbl)
headerView.addSubview(topLbl)
botomLbl.leadingAnchor.constraint(equalTo: headerView.leadingAnchor).isActive = true
botomLbl.trailingAnchor.constraint(equalTo: headerView.trailingAnchor).isActive = true
botomLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
botomLbl.bottomAnchor.constraint(equalTo: headerView.bottomAnchor).isActive = true
topLbl.leadingAnchor.constraint(equalTo: headerView.leadingAnchor).isActive = true
topLbl.trailingAnchor.constraint(equalTo: headerView.trailingAnchor).isActive = true
topLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
topLbl.topAnchor.constraint(equalTo: headerView.topAnchor).isActive = true
botomLbl.text = NULL_STRING
topLbl.text = NULL_STRING
topLbl.backgroundColor = .lightGray
botomLbl.backgroundColor = .lightGray
headingLbl.frame = CGRect.init(x: 0, y: 0, width: headerView.frame.width, height: headerView.frame.height)
headingLbl.text = textHeading
headingLbl.textColor = UIColor(hexString: "587358")
headerView.backgroundColor = UIColor(displayP3Red: 245/256, green: 245/256, blue: 245/256, alpha: 0.6)
headingLbl.textAlignment = .center
headerView.addSubview(headingLbl)
return headerView
}
So, Here is the my code please let me know if i m doing wrong please correct me
Can someone please explain to me hide or show randomly section,
Any help would be greatly appreciated.
Thanks in advance.
CodePudding user response:
class AllDetailAccount {
var HeaderName: String?
var HeaderDeatil: [Any]?
init(HeaderName: String, HeaderDeatil: [Any]) {
self.HeaderName = HeaderName
self.HeaderDeatil = HeaderDeatil
}
}
class UnitUserDetail: AbstractController {
var allAcountDetailInfo = [AllDetailAccount]()
override func numberOfSections(in tableView: UITableView) -> Int {
return allAcountDetailInfo.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if allAcountDetailInfo[section].HeaderName == "USERDETAIL_STRING" {
return 1
}else if allAcountDetailInfo[section].HeaderName == "PHONE_NUMBR_STRING" {
let modelDate = allAcountDetailInfo[section].HeaderDeatil?[0] as! [PhoneNumber]
if modelDate.count == 0 {
return 1
}
return modelDate.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if allAcountDetailInfo[indexPath.section].HeaderName == "USERDETAIL_STRING" {
let modelDate = allAcountDetailInfo[indexPath.section].HeaderDeatil![0] as! UnitUserDetailModel
let cell = tableView.dequeueReusableCell(withIdentifier: "USER_DETAIL_CELL", for: indexPath) as! UserDetail_Cell
cell.updateWith(modelDate, indexPath.row, 0, tblCellType)
return cell
}else if allAcountDetailInfo[indexPath.section].HeaderName == "PHONE_NUMBR_STRING" {
let modelDate = allAcountDetailInfo[indexPath.section].HeaderDeatil?[0] as! [PhoneNumber]
let cell = tableView.dequeueReusableCell(withIdentifier: "PHONE_CELL", for: indexPath) as! PhoneNumberDetailCell
cell.updateWith(modelDate[indexPath.row], indexPath.row, modelDate.count, tblCellType)
return cell
}
let cell = tableView.dequeueReusableCell(withIdentifier: "DEFAULT_CELL", for: indexPath) as! Default_Custom_Cell
return cell
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40))
view.backgroundColor = UIColor(displayP3Red: 245/256, green: 245/256, blue: 245/256, alpha: 0.6)
let lbl = UILabel(frame: CGRect(x: 15, y: 0, width: view.frame.width - 15, height: 40))
lbl.font = UIFont.systemFont(ofSize: 20)
lbl.text = allAcountDetailInfo[section].HeaderName
lbl.textAlignment = .center
lbl.textColor = UIColor(hexString: "HEADER_LABLE_TEXT_COLOUR")
let topLbl = UILabel()
view.addSubview(topLbl)
topLbl.translatesAutoresizingMaskIntoConstraints = false
topLbl.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
topLbl.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
topLbl.heightAnchor.constraint(equalToConstant:0.5).isActive = true
topLbl.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
topLbl.backgroundColor = .lightGray
topLbl.text = NULL_STRING
view.addSubview(lbl)
return view
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
}