swift 在tableview中显示JSON数据时索引超出范围

dgiusagp  于 2022-11-21  发布在  Swift
关注(0)|答案(2)|浏览(173)

我在识别和更改[ListStruct](包含tableview行的初始数据)和[HighlightStruct](包含需要突出显示的name)中包含相同name值的tableview行的颜色时遇到问题。

一开始我用以下JSON数组填充我的tableview:

private func fetchJSON() {
        guard let url = URL(string: "www.test.com")
            else { return }
        var request = URLRequest(url: url)
        request.httpMethod = "POST"
        request.httpBody = "test=test1".data(using: .utf8)
        URLSession.shared.dataTask(with: request) { data, _, error in
            guard let data = data else { return }
            do {
                self.structure = try JSONDecoder().decode([ListStruct].self,from:data)
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }}catch {print(error)}}.resume()}

struct ListStruct: Codable {
    let id: String
    let wo: String
    let name: String
    let type: String
}

然后,同一视图控制器具有第二个JSON数组,该数组在下面进行解码以突出显示:

func processJSON(_ json: String) {
    do{
        let mydata = Data(json.utf8)
        let decoded = try JSONDecoder().decode(Set<HighlightStruct>.self,from: mydata)
        print(decoded)
    } catch {
        print(error)
    }
}

struct HighlightStruct: Codable, Hashable {
    var id: Int
    var name: String
}

应用突出显示

var mySet: Set<HighlightStruct> = []
var highlightedStructure = [HighlightStruct]()
var structure = [ListStruct]()

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! myCell
        
        let portfolio: ListStruct
        portfolio = structure[indexPath.row]
        
        let highlight: HighlightStruct
        highlight = highlightedStructure[indexPath.row]
        
        //Highlight those that match in both arrays
        if highlight.wo == portfolio.wo {
               cell.backgroundColor = .yellow
        }

获取的索引超出范围

pobjuy32

pobjuy321#

您收到索引超出范围错误,因为您的数组为空或数组中不存在索引。也许您可以检查服务调用,数组无法正确填充。

5kgi1eie

5kgi1eie2#

请确保两个列表计数大小相同,或将数据处理到一个列表当结构没有相同WO可比较时,需要处理异常

struct ListStruct: Codable {
    let id: String
    let wo: String
    let name: String
    let type: String
    let hightlight:HighlightStruct!
}

func processJSON(_ json: String) {
    do{
        let mydata = Data(json.utf8)
        let decoded = try JSONDecoder().decode(Set<HighlightStruct>.self,from: mydata)
        print(decoded)
        for hl in decoded{
            var filter = structure.filter({$0.wo == hl.wo})
            filter.hightlight = hl
        }
    } catch {
        print(error)
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! myCell
    
    let portfolio: ListStruct
    portfolio = structure[indexPath.row]
    
    //Highlight those that match in both arrays
    if portfolio.hightlight?.wo == portfolio.wo {
           cell.backgroundColor = .yellow
    }

相关问题