微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

XMLMapper 未在 UITableView 中显示结果

如何解决XMLMapper 未在 UITableView 中显示结果

我遇到了无法从 XMLMapper (objectmapper) 获取 responseData 到 UITableView 的问题。 Tableview 显示 5 个空单元格。有人能帮我解决这个问题吗?

我正在尝试完成一个表视图,其中 IDNR(字符串)显示标签,有效性显示为 DetailTextView

最好的问候,

帕特里克

class PersonDetailsView: UITableViewController {
    
    let redirector = Redirector(behavior: .doNotFollow)
    let RequestUrl = "https://apiurlexample.com/api" 
    var personID:String = ""
    
    var schedules: [cardResult]?
    @IBOutlet weak var tableViewData: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        GetCardNumbers()
    }
    
    class cardResult: XMLMappable {
        
        var nodeName: String!
        
        var error: String?
        var cardrowset: cardRowset?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            error <- map.attributes["error"]
            cardrowset <- map["ROWSET"]
        }
    }
    //
    
    class cardRowset: XMLMappable {
        var nodeName: String!
        
        var cardrows: [cardRow]?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            cardrows <- map["ROW"]
        }
    }
    
    class cardRow: XMLMappable {
        var nodeName: String!
        
        var rcn: String?
        var valid: String?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            rcn <- map["IDNR"]
            valid <- map["VALIDITY"]
        }
    }
    
    func GetCardNumbers() {
        
        //
        class personDetails: XMLMappable {
            var nodeName: String!
            var sql: String?
            
            init() {}
            
            required init?(map: XMLMap) {}
            
            func mapping(map: XMLMap) {
                sql <- (map["sql"],XMLCDATATransform())
            }
        }
        
        let persondetails = personDetails()
        persondetails.nodeName = "query"
        persondetails.sql = "select IDNR,Validity from accesskey WHERE personid=\(personID)"
        
        
        AF.request(RequestUrl,method: .post,parameters: persondetails.toXML(),encoding: XMLEncoding.default)
            .redirect(using: redirector)
            .responseXMLObject { (response: DataResponse<cardResult,AFError>) in
                
                
                switch response.result {
                
                case .success(let value):
                    
                    debugPrint(value)
                    print("AF-Success")
                    
                case .failure(let error):
                    
                    print("AF-Error")
                    print(error)
                    
                }
            }
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        // return the number of rows
        return 5
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)

        let person = schedules?[indexPath.row].cardrowset?.cardrows
        cell.textLabel?.text = person?[indexPath.row].valid

        return cell
    }

解决方法

首先,遵循 Swift 的 API Design Guidelines 总是更好:

遵循大小写约定。类型和协议的名称是 驼峰大写。其他一切都是lowerCamelCase

此外,要在 UITableView 中显示 CardRow 元素数组,您需要将该数组保存在视图控制器中。取而代之的是,您声明了一个由 CardResult 元素组成的数组,这是不正确的,因为只有一个 CardResult 对象。

话虽如此,您的视图控制器应该是这样的:

class PersonDetailsView: UITableViewController {
    
    let redirector = Redirector(behavior: .doNotFollow)
    let requestUrl = "https://apiurlexample.com/api"
    var personID: String = ""
    
    var cardRows: [CardRow] = []
    @IBOutlet weak var tableViewData: UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        getCardNumbers()
    }
    
    class CardResult: XMLMappable {
        var nodeName: String!
        
        var error: String?
        var cardrowset: CardRowset?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            error <- map.attributes["error"]
            cardrowset <- map["ROWSET"]
        }
    }
    
    class CardRowset: XMLMappable {
        var nodeName: String!
        
        var cardrows: [CardRow]?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            cardrows <- map["ROW"]
        }
    }
    
    class CardRow: XMLMappable {
        var nodeName: String!
        
        var rcn: String?
        var valid: String?
        
        required init?(map: XMLMap) {}
        
        func mapping(map: XMLMap) {
            rcn <- map["IDNR"]
            valid <- map["VALIDITY"]
        }
    }
    
    func getCardNumbers() {
        class PersonDetails: XMLMappable {
            var nodeName: String!
            var sql: String?
            
            init() {}
            
            required init?(map: XMLMap) {}
            
            func mapping(map: XMLMap) {
                sql <- (map["sql"],XMLCDATATransform())
            }
        }
        
        let persondetails = PersonDetails()
        persondetails.nodeName = "query"
        persondetails.sql = "select IDNR,Validity from accesskey WHERE personid=\(personID)"
        
        
        AF.request(requestUrl,method: .post,parameters: persondetails.toXML(),encoding: XMLEncoding.default)
            .redirect(using: redirector)
            .responseXMLObject { (response: DataResponse<CardResult,AFError>) in
                switch response.result {
                case .success(let value):
                    debugPrint(value)
                    print("AF-Success")
                    
                    self.cardRows = value.cardrowset?.cardrows ?? []
                    self.tableView.reloadData()
                case .failure(let error):
                    print("AF-Error")
                    print(error)
                }
            }
    }
    
    // numberOfSections(in:) doesn't actually needed here since by default UITableView has one section
    
    override func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        // Here you need to return the count of the cardRows array
        return cardRows.count
    }
    
    override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)

        let person = cardRows[indexPath.row]
        cell.textLabel?.text = person.valid

        return cell
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。