Search code examples
swiftuitableviewuibuttonappendnstimer

reloadData in UITableView on uibutton click - swift


I've been playing around with the UITableView in swift. Firstly, my goal is to append a cell with array data on click of the UIButton.

I've managed to do this with the following action

@IBAction func Option1Click(sender: UIButton) {
arrayOfQuestions.append(QuestionMark(Label:"Grocery List", option1:"yes", option2:"no")) }

But the issue is i cannot get to reload the tableView, because the tableView.reloadData() isn't inherited.

Secondly, i want to apply a timer. So that the appending of the cell would take a specific time interval.

I've managed to find a function on the forums

var timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, 
selector: Selector("Option1Click"), userInfo: nil, repeats: true)

But i don't know where exactly to place this code to work.

Would appreciate any insights greatly!

Thank you.


Solution

  • You should get a reference to your tableview as an outlet and name it whatever you like. After you can use the reference of the tableview and call the reload method like this:

    nameofyourreference.reloadData()