表格视图不显示数据,直到从服务器获取数据后单击屏幕

时间:2019-01-16 07:07:11

标签: ios json swift uitableview

直到我单击屏幕或让它停留很多时间后,屏幕才显示数据 这是我的代码

enter code here

BookMarkVC类:

UIViewController,UITableViewDataSource,UITableViewDelegate {



    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var menuButton: UIBarButtonItem!


 var bookmarks = [DatumForBookMarksMoel]()

    override func viewWillAppear(_ animated: Bool) {
        getAllBookMarks { (succsees) in
            if succsees
            {
                print ("get all book marks ")
                self.tableView.reloadData()

            }
        }

    }

    override func viewDidLoad() {
        super.viewDidLoad()


        menuButton.target  = self.revealViewController()
        menuButton.action = #selector(SWRevealViewController.revealToggle(_:))
        self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        self.view.addGestureRecognizer(self.revealViewController().tapGestureRecognizer())
        tableView.tableFooterView = UIView()
        // Do any additional setup after loading the view.
        tableView.reloadData()
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return bookmarks.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        guard let cell = tableView.dequeueReusableCell(withIdentifier: "bookMarkCell", for: indexPath) as? BookMarkTabelCell else { return UITableViewCell() }
        cell.priceOfItem.text = "\((bookmarks[indexPath.row].offerPrice) ?? 0)"
        cell.depositValue.text = "\(bookmarks[indexPath.row].insuranceValue ?? 0)"
        cell.nameOfItem.text = "\(bookmarks[indexPath.row].name ?? " ")"
        cell.quantity.text = "\(bookmarks[indexPath.row].quantity ?? 0)"
        cell.metookeyerName.text = "\(bookmarks[indexPath.row].accName ?? " ")"
        cell.orderDate.text = "\(bookmarks[indexPath.row].createdAt ?? " ")"



        return cell

    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        return CGFloat(300)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    func getAllBookMarks(completion: @escaping CompletionHandler) {
        let body1: [String: Any] = [
            "draw": 3,
            "length": 100,
            "start": 0
        ]
        print ("get item by category mmmmmmmmm")
    //print("\(URL_ADD_BOOK_MARK)\(itemId)")
      print (AuthService.instance.authToken)
        print(HeaderForLoggedUser)
        Alamofire.request(URL_ALL_BOOK_MARK, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: HeaderForLoggedUser).responseJSON { (response) in
            if response.result.error == nil {
                completion(true)
                // print("login Connection done succes and response data back")
                let data = response.data
                do {
                    completion(true)
                    let result = try JSONDecoder().decode(BookMarksModel.self, from: data!)

                    self.bookmarks = (result.data?.data!)!

             /*       DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }*/
                    DispatchQueue.main.async
                        {
                            self.tableView.reloadData()
                    }


                } catch {
                    completion(false)
                    print(error)
                }
            }
            else {
                completion(false)
                debugPrint(response.result.error as Any)
            }
        }




    }

}
class BookMarkTabelCell : UITableViewCell
{


    @IBOutlet weak var meTooKeyNumber: UILabel!

    @IBOutlet weak var metookeyerName: UILabel!
    @IBOutlet weak var orderDate: UILabel!
    @IBOutlet weak var quantity: UILabel!
    @IBOutlet weak var depositValue: UILabel!
    @IBOutlet weak var nameOfItem: UILabel!

    @IBOutlet weak var priceOfItem: UILabel!


} }

我多次使用reload data()来尝试解决此问题,但不起作用

1 个答案:

答案 0 :(得分:0)

请尝试以下方法:

func getAllBookMarks(completion: @escaping CompletionHandler) {
    let body1: [String: Any] = [
        "draw": 3,
        "length": 100,
        "start": 0
    ]
    print ("get item by category mmmmmmmmm")
//print("\(URL_ADD_BOOK_MARK)\(itemId)")
  print (AuthService.instance.authToken)
    print(HeaderForLoggedUser)
    Alamofire.request(URL_ALL_BOOK_MARK, method: .post, parameters: nil, encoding: JSONEncoding.default, headers: HeaderForLoggedUser).responseJSON { (response) in
        if response.result.error == nil {

            // print("login Connection done succes and response data back")
            let data = response.data
            do {

                let result = try JSONDecoder().decode(BookMarksModel.self, from: data!)

                self.bookmarks = (result.data?.data!)!

                completion(true)



            } catch {
                completion(false)
                print(error)
            }
        }
        else {
            completion(false)
            debugPrint(response.result.error as Any)
        }
    }




}

}

这可能会对您有所帮助。谢谢