How to manage with custom SegmentControllCell to tableview cell?

46 Views Asked by At

I tried to manage from custom segment control cell into ViewController's tableview cell.

This is My custom segment control class:

protocol SegmentControllerCellDelegate: AnyObject {
func manageSegmentControl(cell: SegmentControllerCell)}

class SegmentControllerCell: UITableViewCell, ReusableView, NibLoadableView {

@IBOutlet weak var segmentController: UISegmentedControl!
weak var delegate: SegmentControllerCellDelegate?

override func awakeFromNib() {
    super.awakeFromNib()
    setupUI()
    
}
 @IBAction func tappedSegmentControll(_ sender: UISegmentedControl) {
    self.delegate?.manageSegmentControl(cell: self )
}}

And this is my view controller:

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch BrandSection(rawValue: indexPath.section)! {
    case .profile:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandTableViewCell.self), for: indexPath) as? BrandTableViewCell else { return UITableViewCell() }
        cell.cellType = .brandPage
        return cell
    case .segment:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SegmentControllerCell.self), for: indexPath) as? SegmentControllerCell else { return UITableViewCell() }
        cell.delegate = self
        return cell
    case .products:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandProductionsCell.self), for: indexPath) as? BrandProductionsCell else { return UITableViewCell() }
        return cell
    }
}

extension BrandProfileVC: SegmentControllerCellDelegate {
func manageSegmentControl(cell: SegmentControllerCell) {
   if cell.segmentController.selectedSegmentIndex == 0 {
       self.brandTableView.reloadData()
       print("index 0")
   } else if cell.segmentController.selectedSegmentIndex == 1 {
       self.brandTableView.reloadData()
       print("index 1")
   }
    
}}

I just want to show when I clicked segment control tab, different cell under the segment control.

1

There are 1 best solutions below

0
Wide Angle Technology On

Please try to set the cell.tag as indexPath.row from cellForRow and then try to print the cell.tag inside the manageSegmentControl in your ViewController

This is the Code:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    switch BrandSection(rawValue: indexPath.section)! {
    case .profile:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandTableViewCell.self), for: indexPath) as? BrandTableViewCell else { return UITableViewCell() }
        cell.cellType = .brandPage
        cell.tag = indexPath.row
        return cell
        
    case .segment:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: SegmentControllerCell.self), for: indexPath) as? SegmentControllerCell else { return UITableViewCell() }
        cell.delegate = self
        cell.tag = indexPath.row
        return cell
        
    case .products:
        guard let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: BrandProductionsCell.self), for: indexPath) as? BrandProductionsCell else { return UITableViewCell() }
        cell.tag = indexPath.row
        return cell
        
    }
}

extension BrandProfileVC: SegmentControllerCellDelegate {
    func manageSegmentControl(cell: SegmentControllerCell) {
        if cell.segmentController.selectedSegmentIndex == 0 {
            self.brandTableView.reloadData()
            print(cell.tag)
        } else if cell.segmentController.selectedSegmentIndex == 1 {
            self.brandTableView.reloadData()
            print(cell.tag)
        }
    }
}