代码之家  ›  专栏  ›  技术社区  ›  PvDev

如何在swift中的uiTableViewCell中加载字典数据?

  •  -2
  • PvDev  · 技术社区  · 6 年前

    Dictionary 载入 tableView

    表格视图

    这是我尝试的代码:

     var results: [[String: String]]?
     var currentDictionary: [String: String]? // the current dictionary
     var currentValue: String?                // the current value for one of the 
     keys in the dictionary
     let recordKey = "ItemAttributes"
     let dictionaryKeys = Set<String>(["Title"])
    
      func parserDidStartDocument(_ parser: XMLParser) {
        results = []
      }
    
    // start element
    //
    // - If we're starting a "record" create the dictionary that will hold the results
    // - If we're starting one of our dictionary keys, initialize `currentValue` (otherwise leave `nil`)
    
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        if elementName == recordKey {
            currentDictionary = [:]
        } else if dictionaryKeys.contains(elementName) {
            currentValue = ""
        }
      }
    
    // found characters
    //
    // - If this is an element we care about, append those characters.
    // - If `currentValue` still `nil`, then do nothing.
    
    func parser(_ parser: XMLParser, foundCharacters string: String) {
        currentValue? += string
    }
    
    // end element
    //
    // - If we're at the end of the whole dictionary, then save that dictionary in our array
    // - If we're at the end of an element that belongs in the dictionary, then save that value in the dictionary
    
    func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
        if elementName == recordKey {
            results!.append(currentDictionary!)
            currentDictionary = nil
        } else if dictionaryKeys.contains(elementName) {
            currentDictionary![elementName] = currentValue
            currentValue = nil
        }
     }
    
    // Just in case, if there's an error, report it. (We don't want to fly blind here.)
    
    func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
        print(parseError)
    
        currentValue = nil
        currentDictionary = nil
        results = nil
     }
    
    //         func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    //            return 1
    //        }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
        return results!.count
       }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath as IndexPath) as! amazonProductListTableViewCell
    
        let productlist = ProductName[indexPath.row]
        print("prodic:::\(productlist)")
    
        //        cell.amazonProductTitle?.text = productlist[indexPath.row]
    
        //        cell.detailTextLabel?.text = book.bookAuthor
    
        return cell
     }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   vadian    6 年前

    numberOfRows 在数组初始化应用程序崩溃之前调用。

    results

    var results = [[String: String]]()
    

    parserDidStartDocument 以及 parseErrorOccurred

    results.removeAll()