数据在控制台中加载,但不会在表视图单元格中加载

时间:2017-10-02 12:06:57

标签: ios swift3

这是我的代码,它的工作正常在控制台和数据显示在它,但它 .................................................. ........................在表格视图单元格中没有工作............... 。 我应该怎么做才能按下数据呢? 请帮助............................

 @IBOutlet weak var tMenPro: UITableView!
   var nameArray = [String]()
    var imgURLArray = [String]()
 override func viewDidLoad() {
        super.viewDidLoad()
        self.downloadJsonWithURL()
        //self.nameArray.removeAll()
        self.tMenPro.reloadData()
        tMenPro.delegate =  self
        tMenPro.dataSource = self
        self.slider()
        // Do any additional setup after loading the view .

            }

func downloadJsonWithURL() {
        Alamofire.request("http://...../api/products", method: .post, parameters: ["gender":"Men"], encoding: JSONEncoding.default)
            .responseJSON { (DataResponse) in
                print(DataResponse)
        }
        OperationQueue.main.addOperation({
             self.tMenPro.reloadData()
        })
    }


   func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return nameArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "himCell", for: indexPath) as! HimProud
        cell.himLbl.text = nameArray[indexPath.row]
        //cell.dobLabel.text = adobArray[indexPath.row]

       let imgURL = NSURL(string: imgURLArray[indexPath.row])
        cell.himImg.image = UIImage()
        if imgURL != nil {
            Alamofire.request("http://luxgems.co.uk/api/products", method: .post, parameters: ["main_image":""]).responseData { response in
                debugPrint(response)

                //  print(response.request!)
                //   print(response.response!)
                debugPrint(response.result)

                if let imageData = response.result.value{
                    cell.himImg.image =  UIImage.init(data: imageData)
                }
            }
            let data = NSData(contentsOf: (imgURL as URL?)!)
            cell.himImg.image = UIImage(data: data! as Data)
        }
        return cell

    }

1 个答案:

答案 0 :(得分:0)

tableView重新加载需要成为请求的完成处理程序的一部分。

func downloadJsonWithURL() {
    Alamofire.request("http://luxgems.co.uk/api/products", method: .post, parameters: ["gender":"Men"], encoding: JSONEncoding.default)
        .responseJSON { (DataResponse) in
            print(DataResponse)
            OperationQueue.main.addOperation({
                self.tMenPro.reloadData()
            })
    }
}

当然,您需要填写nameArray