Swift - 初始化 UITableViewCell

Swift - initialize UITableViewCell

我的问题很简单。我只想在没有故事板的情况下以编程方式设置一个 UITableView

我创建了我的 TableView 并且工作正常。问题是 UITableViewCell。它应该只包含一个 UILabel,但我不知道如何在 class 中初始化它。我看过的所有关于 UITableViews 的教程都是在 Storyboard 内完成的。至少是设置。

我是新手,我知道这可能非常简单,但我找不到解决问题的方法。我很感激你的帮助!

    import UIKit

class WhishCell: UITableViewCell {

    var whishText: String

    init(whishText: String){
        self.whishText = whishText
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

更新

这就是我在 UIViewController 中设置 UITableView 的方式:

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
    }()

然后我也 activate 它就像我 UIViewController 中的其他项目一样:

// constrain tableView
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 位于 UIView 内,如果用户点击 button,我会显示它:

cell.wishlistTapCallback = {
        // let wishlistView appear
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: {
            self.wishlistView.transform = CGAffineTransform(translationX: 0, y: 0)
        })}

你在那里做错了一件事。您直接在单元格上设置数据(称为 whishText 的变量)。想一想,愿望的名字属于愿望,不属于细胞。一个人的名字属于一个人,而不属于一个细胞。该单元格只是 presents/shows 给定的任何对象的名称。考虑到这一点。您需要有一个名为 Wish 的 class,并实现 UITableView 数据源方法。这是代码:

许愿细胞:

class WhishCell: UITableViewCell {

    public static let reuseID = "WhishCell"

    required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .systemTeal
        //other common things,
    }
}

愿望表视图控制器:

class WishTableViewController: UITableViewController {

    public var wishList : [Wish]?

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.register(WhishCell.self, forCellReuseIdentifier: WhishCell.reuseID)
    }

    // 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
    }
}

用法示例:

@IBAction func showExampleWishTable(_ sender: Any) {
    let wishTableView = WishTableViewController()

    let wish1 = Wish(withWishName: "Bicycle")
    let wish2 = Wish(withWishName: "Macbook")
    let wish3 = Wish(withWishName: "Printer")

    wishTableView.wishList = [wish1, wish2, wish3]

    self.navigationController?.pushViewController(wishTableView, animated: true)
}

结果: