点击单元格后获取职位发布用户注册头像的技术求助
Hey Juhi, let's work through why you're not getting the job poster's profile avatar and fix this step by step!
The Core Issue
Looking at your code and Postman screenshots, I notice you're currently passing company_logo to the detail view controller—but your goal is to display the job poster's personal registration avatar, not the company's logo.
From the Job Response screenshot, each job entry includes a user_id field. This is the key: you need to use this user_id to fetch the corresponding user's data (including their avatar) from your user API, not pull from the company array.
Step 1: Define a User Data Model
First, create a Codable struct to parse the user API response (adjust field names to match your actual user endpoint):
struct UserData: Codable { let user_id: String let user_avatar: String // Replace with your actual avatar field name from the user API // Add any other user fields you might need }
Make sure your JobData struct also includes the user_id field (since it's returned by the job API) so you can access it later.
Step 2: Fetch User Data & Pass Avatar to Detail View
Update your didSelectRowAt method to fetch the user's avatar using the job's user_id, then pass it to the detail view:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){ let selectedCell = tableView.cellForRow(at: indexPath)! selectedCell.contentView.backgroundColor = .white let jdata = jobFilteredArray[indexPath.row] // Grab the user_id from the selected job guard let userId = jdata.user_id else { print("No user ID found for this job") return } // Request user data from your user API guard let userApiUrl = URL(string: "http://172.16.1.22/Get-User-API/get-user/?user_id=\(userId)") else { print("Invalid user API URL") return } URLSession.shared.dataTask(with: userApiUrl) { [weak self] data, response, error in guard let self = self, error == nil, let data = data else { print("Failed to fetch user data: \(error?.localizedDescription ?? "Unknown error")") return } do { let userData = try JSONDecoder().decode(UserData.self, from: data) // Jump to detail view on main thread DispatchQueue.main.async { let jobDetail = WorkerJobDetailsViewController(nibName: "WorkerJobDetailsViewController", bundle: nil) // Populate all existing fields jobDetail.gender = self.jobArray[indexPath.row].job_emp_gender jobDetail.location = self.jobArray[indexPath.row].job_location jobDetail.companyName = (self.companyArray.first { $0.company_id == jdata.company_id }?.company_name)! jobDetail.skills = self.jobArray[indexPath.row].job_skills jobDetail.descriptionValue = self.jobArray[indexPath.row].job_desc jobDetail.jobDesignation = self.jobArray[indexPath.row].job_desig // Pass the user's avatar instead of company logo jobDetail.profile = userData.user_avatar self.present(jobDetail, animated: true, completion: nil) } } catch { print("Failed to decode user data: \(error)") } }.resume() }
Step 3: Load the Avatar in the Detail View
In WorkerJobDetailsViewController, add code to load the avatar image into your UIImageView. You can use basic URLSession or a library like Kingfisher for better caching/error handling:
Basic URLSession Approach:
override func viewDidLoad() { super.viewDidLoad() // Load avatar if profile path exists guard let avatarPath = profile, let fullAvatarUrl = URL(string: "http://172.16.1.22/\(avatarPath)") else { // Set a default avatar if path is invalid avatarImageView.image = UIImage(named: "default-avatar") return } URLSession.shared.dataTask(with: fullAvatarUrl) { [weak self] data, response, error in guard let self = self, let imageData = data, error == nil else { self?.avatarImageView.image = UIImage(named: "default-avatar") return } DispatchQueue.main.async { self.avatarImageView.image = UIImage(data: imageData) } }.resume() }
Kingfisher Approach (simpler, recommended):
If you use Kingfisher, just add this line to viewDidLoad:
avatarImageView.kf.setImage(with: URL(string: "http://172.16.1.22/\(profile ?? "")"), placeholder: UIImage(named: "default-avatar"))
Bonus Optimization
To avoid making a new API call every time a cell is tapped, you can pre-fetch and cache all user data when you load the job list:
- Collect all unique
user_ids fromjobArray - Make a bulk request to your user API (or individual requests)
- Store user data in a dictionary like
var userCache: [String: UserData] = [:] - Then in
didSelectRowAt, just pull the avatar from the cache instead of making a new request
内容的提问来源于stack exchange,提问作者Juhi Danak

