[英]components on my tableview cell are not visible
I am building a table view contains just a button, with different labels. 我正在建立一个表格视图,其中仅包含一个带有不同标签的按钮。
I am using a table view to present a list of buttons. 我正在使用表格视图来呈现按钮列表。
this a structure containing names of the buttons. 这是一个包含按钮名称的结构。
struct post {
let name : String
}
I have pushed data from firebase to structure post. 我已将数据从Firebase推送到结构发布。
var posts = [post]()
override func viewDidLoad() {
super.viewDidLoad()
let db = Firestore.firestore()
db.collection(Auth.auth().currentUser?.uid as Any as! String).getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
// print("\(document.documentID) => \(document.data())")
self.posts.insert(post(name: document.documentID), at: 0)
}
self.contents.reloadData()
}
}
}
these are normal functions of table view 这些是表格视图的正常功能
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! cellview
cell.programnames.titleLabel?.text = posts[indexPath.row].name
return cell
}
cellview.swift is designed as follow cellview.swift的设计如下
class cellview: UITableViewCell {
@IBOutlet weak var programnames: UIButton!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
Please tell me my mistake in it. 请告诉我我的错误。 Button 'programnames' are not visible in table view in simulator
按钮“程序名”在模拟器的表格视图中不可见
I see a couple of things missing that could be causing UITableViewDataSource
not to be called 我看到缺少一些可能导致
UITableViewDataSource
不被调用的东西
UITableView.dataSource = self
in viewDidLoad
viewDidLoad
设置UITableView.dataSource = self
You need to register cellview.xib
in viewDidLoad
, like so: 您需要在
viewDidLoad
注册cellview.xib
,如下所示:
contents.register(UINib(nibName: "cellview", bundle: nil), forCellReuseIdentifier: "Cell")
Optionals are for the reason that these can be nil
and if it did your app will crash, avoid force unwrapping optionals !
可选参数是因为它们可以
nil
,如果这样做会使您的应用崩溃,请避免强行打开可选参数!
, here is example guard
statement with optional chaining ,这是带有可选链接的示例
guard
声明
guard err == nil, let documents = querySnapshot?.documents else { // error handling here return } self.posts = documents.map { post(name: $0.documentID) }
I saw in your code using posts.insert(..., at: 0)
which will reverse the array, if its on purpose use following instead: 我在您的代码中看到了使用
posts.insert(..., at: 0)
,它将反转数组,如果它是故意使用以下代码的话:
self.posts = documents.map({ post(name: $0.documentID) }).reversed()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.