如何在自定义的json tableView中显示恢复的数据

时间:2018-08-10 09:50:27

标签: ios json swift token

您好,我无法在UILable上的json中显示userProfile的已恢复数据(我自定义了表格视图,问题出在标题中)

我必须开发该功能来检索令牌上的信息(姓氏,名字)

class HPMenuViewController: UITableViewDataSource, UITableViewDelegate {

 @IBOutlet var tableView: UITableView!
    var tableViewModel: HPMenuViewModel?
    let kHPMenuTableViewCell = "HPMenuTableViewCell"
    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?, tableViewModel: HPMenuViewModel?) {
    super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    self.tableViewModel = tableViewModel
    }
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

override func viewDidLoad() {
        super.viewDidLoad()
        self.initTableViewModel()
        self.tableView.tableHeaderView = menuHeaderView
                let menuHeaderView = HPMenuHeaderView.init(frame: CGRect(x: 0, y: 0, width: self.tableView.frame.size.width, height: 200) )
    menuHeaderView.imageName.configuration = ConfigAvatarCircle()
    menuHeaderView.backgroundColor = UIColor.hpCommonBackground()
    menuHeaderView.loadViewModel(viewModel: tableViewModel?.headerMenuViewModel)
        self.tableView.register(UINib(nibName: kHPMenuTableViewCell, bundle: nil), forCellReuseIdentifier: kHPMenuTableViewCell)
}
}

func loadMemberProfil(completion: ((_ sub : [String: AnyObject]) -> Void)!) {
    // get API profile and Bearer token
    let token = HPWSLoginManager.shared().saveSuccessResponse.token
    let url = URL(string: "http://51.38.36.76:40/api/v1/profile")
    var request = URLRequest(url: url!)
    request.httpMethod = "GET"
    request.addValue("Bearer \(token!)", forHTTPHeaderField: "Authorization")
    //get information in token
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        guard let data = data else { return }
        do {
            let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String: AnyObject]
            let sub = json["sub"] as! [String: AnyObject]
            if completion != nil{
              completion(sub)
            }
        } catch {
            print("error")
        }
        }.resume()
}

在这个函数中(我在viewDidLoad()中调用),我想在标题中显示姓名和姓氏

 func initTableViewModel() {

        self.loadMemberProfil { (sub) in

        var arrayCellModel = [HPMenuTableViewCellModel]()
        arrayCellModel.append(HPMenuTableViewCellModel(imageName: "profil",
                                                          titleString: "My Account",
                                                          typeModel: .accountCellModelTypeMyAccount))
        arrayCellModel.append(HPMenuTableViewCellModel(imageName: "request_friends",
                                                          titleString: "Request Friends",
                                                          typeModel: .accountCellModelTypeRequestFriends))

        arrayCellModel.append(HPMenuTableViewCellModel(imageName: "settings",
                                                          titleString: "Settings",
                                                          typeModel: .accountCellModelTypeSettings))

            let headerMenu = HPMenuHeaderViewModel.init(firstName: "\(sub["firstname"]!)", lastName: "\(sub["lastname"]!)")      
            let tableViewModel = HPMenuViewModel(titlePage: "Menu", array: arrayCellModel, headerMenuViewModel: headerMenu)
            self.tableViewModel = tableViewModel

          DispatchQueue.main.async {
                    self.tableView.reloadData()

                }
        }

    }

当我运行应用程序时,它显示表视图单元格数据(我的帐户,设置..),但标题中没有姓氏和名称(UIView在表视图中统一显示)

我需要帮助:(

0 个答案:

没有答案