Search code examples
swiftuitableviewuisegmentedcontrol

how to properly single out cell accessory view


So my goal is to properly separate the cells accessory. Here is my first set of cells when the segment control is at the first index. The accessory type is a normal disclosure indicator.

image1

Now when I switch the value of the segment index, i set my cells with a custom accessory view.

image2

Now the issue is when I switch back to the first segment, the custom accessory view comes over to the first 2 cells as well like so :

image3

I just want to figure out how I can prevent this from happening and keep the cell accessories properly separated. I will attach my necessary code for the issue.

    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
}

The cellForRowAt() method. ^

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

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

The IBAction func for the segmented control. Thanks in advance.


Solution

  • My suspicion is that the accessory view / type is getting carried over on some dequeued cells. You should explicitly set both the accessoryView and cell.accessoryType type in both cases:

    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
        }