简体   繁体   English

标签栏的Navigation VC中ViewController中的ViewController中的Swift TableView:单元格不会填充列表中的数据

[英]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. 我将tableview放置在viewcontroller中,并将其嵌入到导航控制器中,该导航控制器嵌入在标签栏视图控制器中。 The tableview loads but the cells don't have titles. 加载了tableview,但是单元格没有标题。 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. 您需要将视图控制器设置为数据源,并在表视图上调用reloadData。

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? UITableView属性在哪里? You need to declare 您需要声明

@IBOutlet weak var tableView: UITableView!

and connect it to the table view on the storyboard (or .xib file) 并将其连接到情节提要(或.xib文件)上的表格视图

Second 第二

You need to adopt both table view protocols. 您需要同时采用两种表视图协议。

UITableViewDatasource and UITableViewDelegate UITableViewDatasourceUITableViewDelegate

Third 第三

Set the ViewController as delegate 将ViewController设置为委托

tableView.delagate = self tableView.datasource = self tableView.delagate = self tableView.datasource = self

Fourth 第四

As mentioned above, you need to implement func numberOfSections 如上所述,您需要实现func numberOfSections

datasource required method. 数据源必需的方法。

Fifth 第五

Everything should be working. 一切都应该正常工作。 If not, read a bit about UITableView. 如果没有,请阅读有关UITableView的内容。 Because I see that you are not Dequeuing cells on the cellForRowAtIndexPath method. 因为我看到您没有使cellForRowAtIndexPath方法上的单元出队。

Regards 问候

You also need to implement "numberOfSections" dataSource method: 您还需要实现“ numberOfSections”数据源方法:

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类标记为UITableViewDataSource,并将tableView的dataSource属性设置为self:

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


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 移动ViewController时,标签栏和导航栏不会显示 - Tab Bar and Navigation Bar won’t show when the ViewController is moved Swift 4-将数据从ViewController传递到ViewController到标签栏 - Swift 4 - pass data from ViewController to a ViewController to a tab bar Swift - 获取带导航栏和标签栏的viewController的大小 - Swift - get size of viewController with navigation bar and tab bar 在Swift中的选项卡栏中将数据从TableView传递到View Controller - Pass data from tableview to view controller in tab bar in Swift 将数据从tableview传递到Swift中的选项卡栏视图控制器。 - Pass data from tableview to tab bar view controller in Swift. Swift-将数据从TableView传递到第三个ViewController - Swift - pass data from tableview to third viewcontroller Swift 4:将数据从Tableview传递到ViewController - Swift 4: Passing Data from Tableview to ViewController 使用NSArray / PFQuery中的数据填充tableview单元格 - Populate tableview cells with data from NSArray / PFQuery 在标签栏应用程序的表视图中推送新的ViewController - Pushing a new viewcontroller from a tableview in a tab bar app Swift:将数据从ViewController中的tableView传递到另一个ViewController - Swift: Passing data from a tableView in a ViewController to another ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM