80155

tableView.reloadData() Table not updating

The table does not display the updated array to return to the cell. When I launch the app I get blank cells. All permissions have been given in the storyboard. I tried the tableView.reloadData() everywhere but can't seem to make it work either. If anyone can explain where Im going wrong that would really help me get better.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var slider: UISlider! @IBAction func sliderSelector(_ sender: Any) { tableGenerate() } var arrayTable = [Int]() public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayTable.count } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") cell.textLabel?.text = String(arrayTable[indexPath.row]) tableView.reloadData() return cell } func tableGenerate () { var tableMultiplier = 1 while tableMultiplier <= 50 { arrayTable.append(tableMultiplier * Int(slider.value)) tableMultiplier += 1 print(arrayTable) } }

Answer1:

By calling tableView.reloadData() inside cellForRowAt, you create an infinite loop, since reloadData() automatically calls cellForRowAt. You need to move reloadData() inside tableGenerate(), since it should only be called when your data source is changed.

You also need to create an IBOutlet for your tableView in Storyboard. Since you are not using a UITableViewController, you need to do this manually.

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var tableView: UITableView! @IBOutlet weak var slider: UISlider! @IBAction func sliderSelector(_ sender: Any) { tableGenerate() } var arrayTable = [Int]() public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayTable.count } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") cell.textLabel?.text = String(arrayTable[indexPath.row]) return cell } func tableGenerate () { var tableMultiplier = 1 while tableMultiplier <= 50 { arrayTable.append(tableMultiplier * Int(slider.value)) tableMultiplier += 1 print(arrayTable) } tableView.reloadData() } }

Answer2:

Add outlet of tableview like this and connect with table in storyboard-

@IBOutlet weak var tableView1: UITableView!

Change code to this -

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { @IBOutlet weak var slider: UISlider! @IBOutlet weak var tableView1: UITableView! @IBAction func sliderSelector(_ sender: Any) { tableGenerate() } var arrayTable = [Int]() public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arrayTable.count } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier: "Cell") cell.textLabel?.text = String(arrayTable[indexPath.row]) return cell } func tableGenerate () { var tableMultiplier = 1 while tableMultiplier <= 50 { arrayTable.append(tableMultiplier * Int(slider.value)) tableMultiplier += 1 } print(arrayTable) tableView1.reloadData() }

Answer3:

forgot to create the table outlet ! I also forgot to reset the array with the array slider with arrayTable = [] now it works fine

Recommend

  • UIRefreshControl How to detect if a user begins to drag down?
  • Display Firebase Data in UITableView
  • Have UITextField show keyboard (becomeFirstResponder) when UIView is shown
  • NSInternalInconsistencyException while deleting/inserting rows in UITableView
  • CustomCell is empty or results in SIGABRT error [W/Code]
  • UISlider not animating in iOS7
  • Circular UI Slider with Image and Color
  • facebook friend list
  • compare two values of one struct and getting extra cells in UITableView swift 4.0
  • Xcode 10.2 protocol inheritance issue
  • Unable to Decrease CIVignette effect using UISlider
  • swift accessing variable from another file
  • add button in footer tableview in swift
  • UITableView doesn't populate when waiting for CLLocationManagerDelegate 's didUpdateToLoca
  • Creating a Countdown Watch with a slider in Xcode
  • tableView.reloadData() Table not updating
  • Can't get didSelectRowAt to work for TableView
  • How to remove zoom slider in UIImagePickerController
  • WPF Slider control (NullReferenceException)
  • Jquery filter with multiple criteria
  • CSS Bundling and Internet Explorer's Limit
  • Function to check if jquery plugins are already initialized
  • Getting wrong UIButton tag on Long press gesture recognizer
  • playing video in iOS 5
  • How to get a item's coordinates while it is animating?
  • Price range slider for my website
  • Keep transition effect on 1 div from moving the div that follows?
  • Objective C IBOutlets
  • UICollectionView in UITableview - Get tag of tapped UICollectionView
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • Custom Nav Title offset ios 11
  • Question about instantiating object
  • Functions in global context
  • Update CALayer sublayers immediately
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass