2

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...

Rodrigo Otero
  • 222
  • 1
  • 9
  • Please add all your Tableview datasource code. – Aruna Mudnoor Dec 01 '16 at 04:32
  • I'd advise you to use a UIViewController instead, add a view that would have your segmented controls above the tableview. Ensure to set the delegate and datasource to the controller and that the controller conforms to tableview datasource and delegate. Less headache – DrPatience Dec 02 '16 at 14:45

4 Answers4

1

When you reload the tableview, everything added to the table view will be reloaded. Check the selected segment index every time and reset this after table reloading.

sasi kumar
  • 723
  • 5
  • 14
  • That's what I'm doing, but the Selected index only changes when you pull the tableview and not when you click on the segmented control... – Rodrigo Otero May 05 '16 at 15:23
1

U can created a Custom View to replace tableHeaderView, try it.

Allen.Yang
  • 61
  • 3
0

I suspect that the update of the cell is not executed. Is the reloadTable executed on the mainThread?

Include a print log statement in CellForRowAtIndexPath to find out.

Vincent
  • 4,342
  • 1
  • 38
  • 37
0

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.

Rodrigo Otero
  • 222
  • 1
  • 9