Uitableview swift4中tableview内部的可扩展下拉列表
我在swift4中尝试了tableview内部的可扩展下拉列表,但没有使用下面的代码。如果我将自定义模型类更改为字符串,它将正常工作。我的要求使用下面的自定义模型类。请检查我下面的代码Uitableview swift4中tableview内部的可扩展下拉列表,uitableview,swift4,dropdown,expandable,Uitableview,Swift4,Dropdown,Expandable,我在swift4中尝试了tableview内部的可扩展下拉列表,但没有使用下面的代码。如果我将自定义模型类更改为字符串,它将正常工作。我的要求使用下面的自定义模型类。请检查我下面的代码 class ExpandViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate { var empDetails = [EmpWorking
class ExpandViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ExpandableHeaderViewDelegate {
var empDetails = [EmpWorking]()
var hiredDetails = [Hired]()
@IBOutlet weak var tableView: UITableView!
var sections = [SectionData]()
override func viewDidLoad() {
super.viewDidLoad()
loadData()
// Do any additional setup after loading the view.
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return sections[section].emp.count
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if(sections[indexPath.section].expanded){
return 80
}
else{
return 0
}
}
func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 2
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
var header = ExpandableHeaderView()
header.customInit(title: sections[section].genre, section: section, delegate: self)
return header
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "labelCell", for: indexPath) as! expandDataCell
cell.lbl1.text = "\(sections[indexPath.section].emp![indexPath.item].name)"
cell.lbl2.text = "\(sections[indexPath.section].emp![indexPath.item].dept)"
return cell as expandDataCell
}
func toggleSection(header: ExpandableHeaderView, section: Int) {
sections[section].expanded = !sections[section].expanded
tableView.beginUpdates()
for i in 0 ..< sections[section].emp.count
{
tableView.reloadRows(at: [IndexPath(row: i, section: section)], with: .automatic)
}
tableView.endUpdates()
}
func loadData(){
let url = "http://www.json-generator.com/api/json/get/ccgFfBFWqa?indent=2"
guard let resourceString = URL(string: url) else{return}
let decoder = JSONDecoder()
URLSession.shared.dataTask(with: resourceString) { (data, response, err) in
guard let data = data else{return}
do{
let expandData = try decoder.decode([ExpandModel].self, from: data)
print("Expand Data:\(expandData)")
for i in expandData[0].emp_working{
print("i=\(i)")
self.empDetails.append(i)
}
for j in expandData[0].emp_recent_hired{
self.hiredDetails.append(j)
}
DispatchQueue.main.async {
// self.sections = [SectionData(genre: "Employee Working", emp: self.empDetails, expanded: false),
// SectionData(genre: "Employee Hired", emp: self.hiredDetails, expanded: false)
// ]
self.tableView.reloadData()
}
}
catch let jsonErr{
print("Expand Data Err:\(jsonErr)")
}
}.resume()
}
}
struct ExpandModel: Codable{
let emp_working: [EmpWorking]
let emp_recent_hired: [Hired]
}
struct EmpWorking: Codable{
let dept: String
let name: String
}
struct Hired: Codable {
let name: String
let qualification: String
}
struct SectionData {
var genre: String!
var emp: [EmpWorking]!
var expanded: Bool!
init(genre: String, emp: [EmpWorking] = [], expanded: Bool)
{
self.genre = genre
self.emp = emp
self.expanded = expanded
}
}
类ExpandViewController:UIViewController、UITableViewDelegate、UITableViewDataSource、ExpandableHeadServiceWDelegate{
var empDetails=[EmpWorking]()
var hiredDetails=[Hired]()
@IBVAR表格视图:UITableView!
var sections=[SectionData]()
重写func viewDidLoad(){
super.viewDidLoad()
loadData()
//加载视图后执行任何其他设置。
}
func numberOfSections(在tableView:UITableView中)->Int{
返回节数
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回节[section].emp.count
}
func tableView(u tableView:UITableView,heightForHeaderInSection:Int)->CGFloat{
返回44
}
func tableView(tableView:UITableView,heightForRowAt indexath:indexPath)->CGFloat{
if(节[indexPath.section].expanded){
返回80
}
否则{
返回0
}
}
func tableView(tableView:UITableView,heightforfooterInstruction节:Int)->CGFloat{
返回2
}
func tableView(tableView:UITableView,viewForHeaderInSection:Int)->UIView{
var header=ExpandableHeaderView()
header.customInit(标题:节[section]。类型,节:节,委托:self)
回流集管
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
让cell=tableView.dequeueReusableCell(标识符为:“labelCell”,用于:indexath)作为!expandDataCell
cell.lbl1.text=“\(节[indepath.section].emp![indepath.item].name)”
cell.lbl2.text=“\(节[indexPath.section].emp![indexPath.item].dept)”
返回单元格作为expandDataCell
}
func toggleSection(标题:ExpandableHeaderView,小节:Int){
节[section]。已展开=!节[section]。已展开
tableView.beginUpdate()
对于0中的i..<节[section].emp.count
{
tableView.reloadRows(位于:[indepath(行:i,节:section)],带:。自动)
}
tableView.endUpdates()
}
func loadData(){
让url=”http://www.json-generator.com/api/json/get/ccgFfBFWqa?indent=2"
guard let resourceString=URL(string:URL)else{return}
let decoder=JSONDecoder()
URLSession.shared.dataTask(带:resourceString){(数据,响应,错误)在
guard let data=data else{return}
做{
让expandData=try decoder.decode([ExpandModel].self,from:data)
打印(“展开数据:\(展开数据)”)
对于expandData[0]中的i,emp\u正在工作{
打印(“i=\(i)”)
self.empDetails.append(一)
}
用于expandData[0]中的j。emp\u最近\u{
self.hiredDetails.append(j)
}
DispatchQueue.main.async{
//self.sections=[SectionData(类型:“员工工作”,emp:self.empDetails,展开:false),
//SectionData(类型:“雇佣员工”,emp:self.hiredDetails,扩展:false)
// ]
self.tableView.reloadData()
}
}
接球手{
打印(“扩展数据错误:\(JSONER)”)
}
}1.简历()
}
}
结构扩展模型:可编码{
让emp_工作:[emp工作]
让emp_最近雇佣:[雇佣]
}
结构:可编码{
让我们看看:字符串
let name:String
}
结构:可编码{
let name:String
let限定:字符串
}
结构节数据{
类型:字符串!
var emp:[EmpWorking]!
瓦尔:嘘!
初始化(类型:字符串,emp:[EmpWorking]=[],扩展:Bool)
{
self.genre=流派
self.emp=emp
自我扩展=扩展
}
}
所需输出:
员工工作>
雇佣员工>
如果单击Employee Working(员工工作),则会显示以下输出:
雇员工作
约翰
网
亚历克斯
移动的
在上图中,您可以看到动态数据将出现在两个标签上。在每个单元格索引上,单击它将展开单元格,如下图所示
因此,您只需执行以下步骤:-
首先创建一个变量,用于存储didselectrowat索引,例如:-
var int_row = Int()
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
int_row = indexPath.row . // save the index path
yourTableViewName.reloadData() // reload the table
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if int_row == indexPath.row { // when you tap on cell then it will expend
the cell
return UITableView.automaticDimension
} else {
return 48 // at this time only show your uppar labal only
}
}
你的问题不清楚。你能提供更多关于你想要实现什么的信息吗?是否希望在tableview“Exp working”和“Hired”中有两个部分,如果单击该部分,将显示单元格。。基本上是膨胀和崩溃?