如何给UITableView中的每个单元格类别指定特定的部分名称

我正在用两种类型的单元格构建用户配置文件表视图。 第一个单元格包含一个图像视图和一个标签(分别用于用户图像及其名称)

第二个并排包含2个标签(一个用于职位名称,另一个用于职位描述)。

我希望达到的结果是:

  • 每个单元格类型都在一个部分下
  • 第一部分仅包含一个单元格(图像和标签)
  • 第二部分包含3个单元格(第二种类型)。

我设法实现的目标是:

  • 我有正确的栏目名称/标题
  • 我的部分数正确
  • 第1节只有一种类型的单元格(正确的类型)
  • 但是在第2节中,我同时拥有第一种和第二种细胞。

我想从第2节中删除第一种单元格。这是我的代码:

//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
    }

  
iCMS 回答:如何给UITableView中的每个单元格类别指定特定的部分名称

您需要使用indexPath.section == 0检查该部分是否是第一个部分,而不是使用indexPath.row == 0检查该行以通过cellForRow方法返回适当的单元格。

替换:

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

使用:

override func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.section == 0 {
本文链接:https://www.f2er.com/2044140.html

大家都在问