如何正确挑选单元格附件视图

问题描述

所以我的目标是正确分离细胞附件。当段控件位于第一个索引处时,这是我的第一组单元格。附件类型是一个正常的披露指示器。

image1

现在,当我切换段索引的值时,我使用自定义附件视图设置了我的单元格。

image2

现在的问题是,当我切换回第一段时,自定义附件视图也会像这样转到前 2 个单元格:

image3

我只想弄清楚如何防止这种情况发生并保持电池附件正确分开。我会附上这个问题所需的代码

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellDetails.studentDashCell,for: indexPath)
    let cellImage = UIImage(systemName: "checkmark.seal.fill")
    let cellImageView = UIImageView(image: cellImage)
    cellImageView.tintColor = #colorLiteral(red: 0.4666666687,green: 0.7647058964,blue: 0.2666666806,alpha: 1)
    cellImageView.frame = CGRect(x: 0,y: 0,width: 30,height: 30)
    
    
    
    if selectorOfEvents.selectedSegmentIndex == 0 {
        cell.textLabel?.font = UIFont(name: Constants.AppFonts.consistentFont,size: 22)
        cell.textLabel?.text = gothereEvents[indexPath.row].eventName
        cell.accessoryType = .disclosureIndicator
    } else if selectorOfEvents.selectedSegmentIndex == 1 {
        cell.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont,size: 22)
        cell.textLabel?.text = gotherePurchasedEventNames[indexPath.row].purchasedEventName
        cell.accessoryView = cellImageView
        

    }
    
    return cell
}

cellForRowAt() 方法。 ^

 @IBAction func eventsSegmented(_ sender: UISegmentedControl) {
    if sender.selectedSegmentIndex == 0 {
        navigationItem.title = "Events"
        tableView.reloadData()

     
    } else {
        navigationItem.title = "Purchased Events"
        tableView.reloadData()
    }
    
}

分段控件的 IBAction 函数。提前致谢。

解决方法

我怀疑附件视图/类型在一些出列的单元格上被保留了下来。在这两种情况下,您都应该明确设置 accessoryViewcell.accessoryType 类型:

if selectorOfEvents.selectedSegmentIndex == 0 {
        cell.textLabel?.font = UIFont(name: Constants.AppFonts.consistentFont,size: 22)
        cell.textLabel?.text = gothereEvents[indexPath.row].eventName
        cell.accessoryType = .disclosureIndicator
        cell.accessoryView = nil //<-- here
    } else if selectorOfEvents.selectedSegmentIndex == 1 {
        cell.textLabel?.font = UIFont(name: Constants.AppFonts.menuTitleFont,size: 22)
        cell.textLabel?.text = gotherePurchasedEventNames[indexPath.row].purchasedEventName
        cell.accessoryView = cellImageView
        cell.accessoryType = .none //<-- here
    }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...