iosswiftxcodeuitableviewuitableviewsectionheader

how to give a specific section name to each category of cell in a UITableView


i am building a user profile table view with 2 types of cell. the first cell contains an image view and a label (for the user image and their name, respectively)

the second one contains 2 labels side by side (one for a job title, and the other for the job description).

The result I wish to achieve is :

What I have managed to achieve is :

I would like to remove the first type of cell from section 2. Here is my code :

//MARK: - 2 CELLS CLASS

class ImageCell: UITableViewCell {
    @IBOutlet weak var candidatePicture: UIImageView!
    @IBOutlet weak var candidateNameLabel: UILabel!
    
}

//

class ItemCell: UITableViewCell {
    @IBOutlet weak var itemNameLabel: UILabel!
    @IBOutlet weak var itemDetailLabel: UILabel!
    
}

class CandidateUserProfileScreenTableViewController: UITableViewController {
    
    //MARK: RELEVANT VARIABLES
    
    let sectionNameArray: [String] = ["Candidate ID", "Jobs"]
    let candidateId = ["Name"]
    let candidateJobs = ["job 1", "job2", "job3"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return sectionNameArray.count
    }

    
    override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        //return sectionNameArray[0]
        return sectionNameArray[section]
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        //return 4
        
        switch section {
        case 0:
            return candidateId.count
        case 1:
            return candidateJobs.count
        default:
            fatalError("there was a mistake in my code")
        }
    }

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

        // en fonction de index path
        // index path == 0 on affiche la cellule avec image
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "imageCell", for: indexPath) as! ImageCell
            
            //
            
            cell.candidatePicture.image = UIImage(named: "welcome")
            cell.candidateNameLabel.text = "John SMITH"
            
            return cell
        }
        // sinon on affiche le second type de cellule
        let cell = tableView.dequeueReusableCell(withIdentifier: "itemCell", for: indexPath) as! ItemCell
        
        cell.itemNameLabel.text = "Job 1"
        cell.itemDetailLabel.text = "Job 1 detail"
        
        return cell
    }

  

Solution

  • You need to check if the section is the first one with indexPath.section == 0 instead of checking the row with indexPath.row == 0 for returning the appropriate cell in cellForRow method.

    Replace:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
    

    With:

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {