我有一个UITableView
,我正在尝试添加一些内容(Whish
)。我已经以编程方式设置了所有内容,但是以某种方式添加cells
对我来说不起作用。
这是我的数据UITableView
自定义Wish
类:
import UIKit
class WhishlistTableViewController: UITableViewController {
public var wishList : [Wish]?
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.register(WhishCell.self, forCellReuseIdentifier: WhishCell.reuseID)
self.wishList?.append(Wish(withWishName: "Test"))
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return wishList?.count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: WhishCell.reuseID, for: indexPath)
let currentWish = self.wishList![indexPath.row]
cell.textLabel?.text = currentWish.wishName
return cell
}
}
class Wish: NSObject {
public var wishName : String?
init(withWishName name: String) {
super.init()
wishName = name
}
}
这就是我WishlistTableView
在其他人内部创建的方式UIViewController
:
let theTableView: WhishlistTableViewController = {
let v = WhishlistTableViewController()
v.view.layer.masksToBounds = true
v.view.layer.borderColor = UIColor.white.cgColor
v.view.layer.borderWidth = 2.0
v.view.translatesAutoresizingMaskIntoConstraints = false
return v
}()
及其constraints
:
theTableView.view.topAnchor.constraint(equalTo: wishlistView.topAnchor, constant: 180.0),
theTableView.view.bottomAnchor.constraint(equalTo: wishlistView.bottomAnchor, constant: 0),
theTableView.view.leadingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.leadingAnchor, constant: 30.0),
theTableView.view.trailingAnchor.constraint(equalTo: wishlistView.safeAreaLayoutGuide.trailingAnchor, constant: -30.0),
在TableView
与它的出现只是罚款constraints
,但加cells
不工作,我不知道为什么..为感谢每一个帮助:)
更换
public var wishList : [Wish]?
与
public var wishList = [Wish]()
由于您从未初始化过,wishList
因此在此处添加项目wishList?.append
不会做任何事情
self.wishList.append(Wish(withWishName: "Test"))
也将其删除,因为它是默认设置
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句