uitableview - swift4中tableview内的可扩展下拉菜单
问题描述
我在 swift4 的 tableview 中尝试了可扩展的下拉菜单,但没有使用下面的代码。如果我将自定义模型类更改为 String 它正在工作。我的要求在自定义模型类下面使用。请检查我的以下代码。
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
}
}
所需输出:
员工工作 > 员工雇用 >
如果点击员工工作,那么它将显示以下输出:
员工工作 V John Web
亚历克斯移动
解决方案
在上图中,您可以看到将出现动态数据的两个标签。在每个单元格索引上单击它将展开单元格,如下面的屏幕所示。
所以你需要做这些步骤:-首先创建一个变量来存储前的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
}
}
推荐阅读
- cors - 主页未加载
- authorization - Blazor wasm 托管'无法从配置/TestApp.Client 加载设置-(AuthenticationService.js)
- ionic-framework - Flurry Analytics 是否支持华为设备上没有 Playstore/GMS 服务的标志性应用程序?
- python - 如何根据某些条件删除存储在numpy数组中的图像参考作为字符串
- mesibo - Angular 7 中的 Mesibo - 会议本地流媒体不起作用
- powershell - 通过powershell删除IE浏览器历史
- julia - Julia shell 命令如何设置环境变量?
- python - 硒中的元素迭代
- ios - 设备锁定后如何在 iOS 应用中录制视频?
- android - 使用 Jenkins 构建 Gradle 项目时出错