[英]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... 哇,该代码有很多错误...
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文件)上的表格视图
You need to adopt both table view protocols. 您需要同时采用两种表视图协议。
UITableViewDatasource
and UITableViewDelegate
UITableViewDatasource
和UITableViewDelegate
Set the ViewController as delegate 将ViewController设置为委托
tableView.delagate = self
tableView.datasource = self
tableView.delagate = self
tableView.datasource = self
As mentioned above, you need to implement func numberOfSections
如上所述,您需要实现func numberOfSections
datasource required method. 数据源必需的方法。
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.