On cell click display image of particular logged in id

Deadly 提交于 2019-12-25 01:35:15

问题


I need to print image in my next view controller after comparing ID of a table containing user details after comparing the ID I am successfully getting the name but the respective image is unable to fetch if the user has posted anything then I am getting name for particular posted job now what I want is image of from respective user (that image which user uploaded while registration), (which identifies the posted job is posted via which user).

Below is my code:

func getJOBData()
    {
        let jobUrl = URL(string: "http://172.16.1.22/Get-Job-API/get-jobs/")
        URLSession.shared.dataTask(with: jobUrl!) { (data, response, error) in

            do
            {
                if error == nil
                {
                    self.jobArray = try JSONDecoder().decode([JobData].self, from: data!)

                    for mainJobArr in self.jobArray
                    {
                        DispatchQueue.main.async {
                            self.jobPostTblView.reloadData()
                        }
                    }
                    print("Job Data****\(self.jobArray)")
                }
            }

            catch
            {
                //                print("my data=\(self.mainCellData)")
                print("Error in get JSON Data\(error)")
            }
            }.resume()
    }

numberOfRowsInSection

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

            return jobFilteredArray.count
        }

cellForRowAtIndexPath Method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = Bundle.main.loadNibNamed("JobTableViewCell", owner: self, options: nil)?.first as! JobTableViewCell

            let data = jobArray[indexPath.row]
            cell.jobTitle.text = data.job_desig
            cell.expDetails.text = data.job_exp_to
            cell.locationDetails.text = data.job_location
            cell.dateDetails.text = data.job_skills
            cell.companyName.text = companyArray.first { $0.company_id == data.company_id }?.company_name

return cell

        }

didSelectRowAtIndexPath

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
                let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
                selectedCell.contentView.backgroundColor = UIColor.white

                let rows = indexPath.row
                print("Rows=\(rows)")
                let jobDetail = WorkerJobDetailsViewController(nibName: "WorkerJobDetailsViewController", bundle: nil)

                let jdata = jobFilteredArray[indexPath.row]
                jobDetail.gender = jobArray[indexPath.row].job_emp_gender
                jobDetail.location = jobArray[indexPath.row].job_location
                jobDetail.companyName = (companyArray.first { $0.company_id == jdata.company_id }?.company_name)!

                jobDetail.profile = jobImgPath
                jobImgPath = (companyArray.first { $0.company_id == jdata.company_id }?.company_logo)!

                jobDetail.skills = jobArray[indexPath.row].job_skills
                jobDetail.descriptionValue = jobArray[indexPath.row].job_desc
                jobDetail.jobDesignation = jobArray[indexPath.row].job_desig

                self.present(jobDetail, animated: true, completion: nil)
            }

Can anyone please help me to fetch images for respective user of posted job??


回答1:


just use a pod like SDWebImage and fetch the link with the url that you are mapping the problem that you will face is that the link is a local ip and it will not work from a remote network but if the link changes at the json in the future you will be fine

Without pod you can do this

    func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
    URLSession.shared.dataTask(with: url) { data, response, error in
        completion(data, response, error)
    }.resume()
    }

    func downloadImage(url: URL) {
    print("Download Started")
    getDataFromUrl(url: url) { data, response, error in
        guard let data = data, error == nil else { return }
        print(response?.suggestedFilename ?? url.lastPathComponent)
        print("Download Finished")
        DispatchQueue.main.async() {
            self.imageView.image = UIImage(data: data)
        }
      }
   }


来源:https://stackoverflow.com/questions/50448806/on-cell-click-display-image-of-particular-logged-in-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!