简体   繁体   中英

Swift TableView in ViewController In Navigation VC in Tab Bar: Cells won't populate with data from list

I can't seem to populate my cells in my tableview with titles. I placed my tableview in my viewcontroller and embedded that in a navigation controller which is embedded in a tab bar view controller. The tableview loads but the cells don't have titles. I'm not sure what I'm doing wrong.

            import Foundation
            import UIKit

            class ThirdViewController: UIViewController {

                let tableData = ["One","Two","Three"]

                override func viewDidLoad() {
                    super.viewDidLoad()
                    // Do any additional setup after loading the view, typically from a nib.
                    navigationController?.navigationBar.barTintColor = hexStringToUIColor(hex: "#00a5ff")
                    navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Futura", size: 25)!]
                    self.title = "Interests"
                }

                func tableView(_ tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
                {
                    return tableData.count
                }

                func tableView(_ tableView: UITableView!,
                               cellForRowAtIndexPath indexPath: IndexPath!) -> UITableViewCell!
                {

                    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.default, reuseIdentifier:"Cell")

                    cell.textLabel?.text = tableData[indexPath.row]

                    return cell
                }

                func hexStringToUIColor (hex:String) -> UIColor {
                    var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()

                    if (cString.hasPrefix("#")) {
                        cString.remove(at: cString.startIndex)
                    }

                    if ((cString.characters.count) != 6) {
                        return UIColor.gray
                    }

                    var rgbValue:UInt32 = 0
                    Scanner(string: cString).scanHexInt32(&rgbValue)

                    return UIColor(
                        red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
                        green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
                        blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
                        alpha: CGFloat(1.0)
                    )
                }
            }

You need to setup your view controller as the datasource and call reloadData on your table view.

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view, typically from a nib.
  navigationController?.navigationBar.barTintColor = hexStringToUIColor(hex: "#00a5ff")
  navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white, NSFontAttributeName: UIFont(name: "Futura", size: 25)!]
  self.title = "Interests"

  yourTableView.dataSource = self

  yourTableView.reloadData()
}

Wow, there's so many mistakes in that code...

First

Where's the UITableView property? You need to declare

@IBOutlet weak var tableView: UITableView!

and connect it to the table view on the storyboard (or .xib file)

Second

You need to adopt both table view protocols.

UITableViewDatasource and UITableViewDelegate

Third

Set the ViewController as delegate

tableView.delagate = self tableView.datasource = self

Fourth

As mentioned above, you need to implement func numberOfSections

datasource required method.

Fifth

Everything should be working. If not, read a bit about UITableView. Because I see that you are not Dequeuing cells on the cellForRowAtIndexPath method.

Regards

You also need to implement "numberOfSections" dataSource method:

func numberOfSections(in tableView: UITableView) -> Int
{
  return 1
}

Other than that, you need to mark your ThirdViewController class as a UITableViewDataSource, and set dataSource property of the tableView to self:

ThirdViewController: UIViewController, UITableViewDataSource
{
     override func viewDidLoad() {
     super.viewDidLoad()
     ........
     yourTableView.dataSource = self


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM