Karl Kivi Karl Kivi - 1 year ago 51
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

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
method. I have initialised tableview and segmentedControl button in CustomView init() method. The purpose of loading the data in
is because when user selects specific segment the tableview should refresh with new data.

Main struggle is how to access the tableview from

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

var customSegmentedB = UISegmentedcontrol(blah)
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
// Also reload data


Answer Source

Just add self.tableView.reloadData()