【问题标题】:Section and Row not properly displayed in UITableView Swift部分和行在 UITableView Swift 中未正确显示
【发布时间】:2020-05-27 18:57:27
【问题描述】:

我正在尝试在 UITableView 中实现部分和行,但由于嵌套的 JSON 模型结构,我没有成功。它总是只打印first section title。我正在使用Codable 方法并且模型结构无法更改。

如何在tableView 中实现部分和行?任何指导或帮助将不胜感激。我真的为此而苦苦挣扎。

  • 我需要在 tableView 部分显示 title 和 Rows textField -- 请参阅 JSON。
  • 附上只打印一张的截图section title

型号:

struct SectionList : Codable {

    let title : String?
    var items : [Item]?

}

struct Item : Codable {

    let actionType : Int?
    var textField : String?
    let pickList: [SectionList]?
    let itemValue: String?
    let version: Int?

}

初始化&TableView代码:

var AppData: [Item]?

let decoder = JSONDecoder()
let response = try decoder.decode(SectionList.self, from: pickResult)
let res = response.items?.filter { $0.actionType == 101}
self.AppData = res


func numberOfSections(in tableView: UITableView) -> Int {
        return AppData?.count ?? 0
    }

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
   return AppData?[section].pickList[0].title
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return AppData?[section].pickList?.count ?? 0
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()
    let dic = AppData?[indexPath.section]//.pickList?[indexPath.row].title
        //AppData?[indexPath.section].pickList[indexPath.row].items
    //print(dic)

    return cell 
}

【问题讨论】:

    标签: ios json swift uitableview


    【解决方案1】:

    无论你怎么理解,我都会做到。检查此代码

    创建结构为

    struct SectionList : Codable {
    
        let title : String?
        var items : [RowItems]?
    
    }
    
    struct RowItems: Codable {
        var textField : String?
        let itemValue: String?
    }
    
    struct SourceData: Codable {
        let items: [Item]?
    }
    struct Item : Codable {
        let actionType : Int?
        let pickList: [SectionList]?
        let version: Int?
    
    }
    

    像这样创建变量

    var AppData: Item?
    

    将 json 解析为

    let decoder = JSONDecoder()
                let response = try decoder.decode(SourceData.self, from: data)
                let res = response.items?.filter { $0.actionType == 101}
                print("jsonData:\(res)")
                AppData = res?.first
    

    调用表数据源为

    func numberOfSections(in tableView: UITableView) -> Int {
        return AppData?.pickList?.count ?? 0
        }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return AppData?.pickList?[section].title
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return AppData?.pickList?[section].items?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        let dic = AppData?.pickList?[indexPath.section].items?[indexPath.row]//.pickList?[indexPath.row].title
            //AppData?[indexPath.section].pickList[indexPath.row].items
        //print(dic)
    
        cell.textLabel?.text = dic?.textField
        return cell
    }
    

    此代码的屏幕截图

    【讨论】:

    • 参考你的回答,我可以问一个与searchBar相关的问题吗?如果我使用TextField 作为搜索栏,我该如何过滤数据,你能指导我吗?数据相同。
    • 您要在什么基础上过滤数据?文本字段?
    • 相同的 TextField 值显示 let dic = AppData?.pickList?[indexPath.section].items?[indexPath.row] dic?.textField
    • 我有这个工作和测试的解决方案,我会发布它的问题。
    • 请查收,我已经回复了。
    【解决方案2】:

    你的模型应该是这样的

    import Foundation
    
        // MARK: - Items
        struct Items: Codable {
            let items: [ItemsItem]?
        }
    
        // MARK: - ItemsItem
        struct ItemsItem: Codable {
            let actionType, version: Int?
            let pickList: [PickList]?
        }
    
        // MARK: - PickList
        struct PickList: Codable {
            let title: String?
            let items: [PickListItem]?
        }
    
        // MARK: - PickListItem
        struct PickListItem: Codable {
            let textField, itemValue: String?
        }
    

    你的变量应该是这样的

    var appData : Items?
    

    在解码时添加这个

    appData = try decoder.decode(Items.self, from: pickResult)
    

    你的表视图委托和数据源应该像

      func numberOfSections(in tableView: UITableView) -> Int {
                    return appData?.items?.first?.pickList?.count ?? 1
                }
                func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
                    let cell = UITableViewCell()
                    return appData?.items?.first?.pickList?.first?.title
                }
                func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                    return appData?.items?.first?.pickList?[section].items?.count ?? 1
                }
    
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                    let cell = UITableViewCell()
                    cell.textLabel?.text = appData?.items?.first?.pickList?.first?.items?[indexPath.row].textField
                    return cell
                }
    

    【讨论】:

      猜你喜欢
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多