Search code examples
iosswiftuitableviewuisegmentedcontroluitableviewsectionheader

UISegmentedControl behaves in a weird way on UITableView Header


I created a Custom Cell to replace UITableViewSectionHeader with a protocol to process when UISegmentedControl.index is changed so i can order the table by name or value:

Custom Cell

import UIKit
protocol OrdenarTableViewDelegate {
func ordenarTableView(cell: OrdenarTableViewCell)
}
class OrdenarTableViewCell: UITableViewCell {
var delegate: OrdenarTableViewDelegate?
@IBOutlet weak var segmentedOrdenar: UISegmentedControl!
@IBAction func alteraOrdenacao(sender: UISegmentedControl) {
    delegate?.ordenarTableView(self)
}

On the UITableViewControler I referenced the protocol

class SelecionadasTableViewController: UITableViewController, UITextFieldDelegate, OrdenarTableViewDelegate {

and implemented it:

    // MARK: - Ordenar Delegate
func ordenarTableView(cell: OrdenarTableViewCell) {
    if cell.segmentedOrdenar.selectedSegmentIndex == 0 {
        listaCervejas = bancoDeDados.selecionaCervejas(false)
    } else {
        listaCervejas = bancoDeDados.selecionaCervejas(true)
    }
    ordenarCervejas = cell.segmentedOrdenar.selectedSegmentIndex
    tableView.reloadData()
}

When I select the index 1, the app works just fine, but when index 0 is selected the action is only performed if click on a UITextField on any other cell or I pull the table down for refreshing (which by the way is not implemented to refresh)

Is there anything that I'm missing? Because it just feels really awkward.


I notice that the problem only occurs when I click one option and then the other on the UISegmentedControl without touching nowhere else on the screen...


Solution

  • I actually found the solution. You can drag the IBAction to the tableViewController instead. Then it works just fine!! Thank you all for the help.