Arrays 根据服务器响应隐藏显示部分
我正在开发一个应用程序,它使用多个部分和多个标题视图的表视图。是否可以根据服务器响应随机隐藏节,即:我有6个多单元格和标题视图。我从服务器得到的响应只显示了2个部分,4个部分和6个部分。我试图做到这一点,但没有成功。下面是我的代码: 代码:Arrays 根据服务器响应隐藏显示部分,arrays,swift,xcode,uitableview,sections,Arrays,Swift,Xcode,Uitableview,Sections,我正在开发一个应用程序,它使用多个部分和多个标题视图的表视图。是否可以根据服务器响应随机隐藏节,即:我有6个多单元格和标题视图。我从服务器得到的响应只显示了2个部分,4个部分和6个部分。我试图做到这一点,但没有成功。下面是我的代码: 代码: var allPermissionSection = ["A","B","C","D","E","F"] // default permission override func numberOfSections(in tableView: UITable
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
}
var allPermissionSection=[“A”、“B”、“C”、“D”、“E”、“F”]//默认权限
重写func numberOfSections(在tableView:UITableView中)->Int{
return totalSectionTable//显示单元格的总计数
}
重写func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
将modelPermission=mainArrayCheckValidations[0]设为!MyAcountModel
如果节==0{
返回1
}
//从服务器检查索引中获取的arraySectionName如果默认节或服务器响应索引等于相同,则为true继续
如果allPermissionSection[0]==arraySectionName[0]{
如果ArrayBlItems.count>0{
返回ArrayBlItems.count
}否则,如果modelPermission.PhoneNumberEnabled==true{
返回1
}
}
如果allPermissionSection[1]==arraySectionName[1]{
如果ArrayBlItemsFolectronic.count>0{
返回ArrayBlItemsforectronic.count
}如果modelPermission.econsentEnabled==true,则为else{
返回1
}
}
如果allPermissionSection[2]==arraySectionName[2]{
如果ArrayBlItemsForemergenceContact.count>0{
返回ArrayBlItemsForemergenceContact.count
}否则,如果modelPermission.emergencyContactsEnabled==true{
返回1
}
}
返回0
}
重写func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
将modelPermission=mainArrayCheckValidations[0]设为!MyAcountModel
如果indexath.section==0{
返回UITableView.automaticDimension
}
如果allPermissionSection[0]==arraySectionName[0]{
//如果indexath.section==1{
如果ArrayBlItems.count为0{
让cell=tableView.dequeueReusableCell(标识符为:consent\u cell,for:indexPath)作为!consent\u cell
cell.updateWith(ArrayBlItemsFolectronic[indexPath.row],indexPath.row,ArrayBlItemsFolectronic.count,tblCellType)
返回单元
}否则{
让cell=tableView.dequeueReusableCell(带有标识符:DEFAULT\u cell,for:indexath)作为!DEFAULT\u Custom\u cell
cell.lbl_Name.text=“”
返回单元
}
// }
}
如果allPermissionSection[2]==arraySectionName[2]{
//如果indexath.section==3{
如果ArrayBlItemsForemergenceContact.count>0{
让cell=tableView.dequeueReusableCell(标识符:EMERGENCY\u CONTACT\u cell,for:indexPath)作为!emergencyccnctcell
cell.updateWith(ArrayBlItemsForemergnceContact[indexPath.row],indexPath.row,ArrayBlItemsForemergnceContact.count,tblCellType)
返回单元
}否则{
让cell=tableView.dequeueReusableCell(带有标识符:DEFAULT\u cell,for:indexath)作为!DEFAULT\u Custom\u cell
cell.lbl_Name.text=“”
返回单元
}
// }
}
返回UITableViewCell()
}
重写func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView?{
如果节==0{
let headerView=setupHeaderView(文本标题:“详细信息”)
返回头视图
}
如果allPermissionSection[0]==arraySectionName[0]{
//如果节==1{
let headerView=setupHeaderView(文本标题:“BER”)
返回头视图
// }
}
如果allPermissionSection[1]==arraySectionName[1]{
//如果节==2{
let headerView=setupHeaderView(文本标题:“已发送”)
返回头视图
// }
}
如果allPermissionSection[2]==arraySectionName[2]{
//如果节==3{
let headerView=setupHeaderView(文本标题:“ontacts”)
返回头视图
// }
}
返回UIView()
}
重写func tableView(tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
如果节==0{
返回50
}
如果allPermissionSection[0]==arraySectionName[0]{
//如果节==1{
返回50
// }
}
如果allPermissionSection[1]==arraySectionName[1]{
//如果节==2{
返回50
// }
}
如果allPermissionSection[1]==arraySectionName[1]{
//如果节==3{
返回50
// }
}
返回0
}
setupHeaderView(文本标题:字符串)->UIView?{
让headerView=UIView.init(帧:CGRect.init(x:0,y:0,宽度:tblVw.frame.width,高度:50))
let headingLbl=UILabel()
设topLbl=UILabel()
让botomLbl=UILabel()
topLbl.translatesAutoresizingMaskIntoConstraints=false
botomLbl.translates自动调整大小gmaskintoConstraints=false
headerView.addSubview(botomLbl)
headerView.addSubview(topLbl)
botomLbl.leadingAnchor.constraint(等式: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(等于: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
机器人程序
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
}
}