Karl Kivi Karl Kivi - 3 months ago 8
iOS Question

How to reload Tableview data from another method

Does anyone has any idea how to structure my code around that I could access the tableview variable and reload the data from

changevalues
method. Or gives me some tips how to do it, because I have run out of ideas and I don't even know what to google anymore. Thanks

I made a code sample that shows my situation. My issue: I want to reload tableview data in
changevalues
method. I have initialised tableview and segmentedControl button in CustomView init() method. The purpose of loading the data in
changevalues
is because when user selects specific segment the tableview should refresh with new data.

Main struggle is how to access the tableview from
changevalues
method?

Both UITableView and UISegmentedControl are subviews of CustomView

if my question is duplicate, please refer to that question, because I have searched for the answer to my problem, but with any luck.

class CustomView: UIView, UITableViewDataSource, UITableViewDelegate {

var changingTableviewData = []

override init(frame: CGRect) {
var tableView = UITableView(blah...)
tableview.background...
tableview.registercell...
tableview.delegate..

var customSegmentedB = UISegmentedcontrol(blah)
customSegmentedB.frame...
customsegmentedB.addTarget(self, action: "changevalues:", forControlEvents: .ValueChanged)

}


func changevalues(sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 1:
// I want to reload tableview Data
case 2:
// Also reload data
default:
// Also reload data
}
}



}

Answer

Just add self.tableView.reloadData()

Comments