ios - Swift - 初始化 UITableViewCell

标签 ios swift uitableview initialization

我的问题很简单..我想要的只是以编程方式设置一个UITableView,而不需要 Storyboard。

我创建了我的 TableView 并且工作正常。问题是 UITableViewCell。它应该只包含一个 UILabel 但我不知道如何在类中初始化它。我看过的所有关于 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
    }()

然后我也像 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 中,如果用户点击 按钮,我会显示它:

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

最佳答案

你在那里做错了一件事。您直接在单元格上设置数据(称为 whishText 的变量)。想一想,愿望的名字属于愿望,不属于细胞。一个人的名字属于一个人,而不属于一个细胞。该单元格只是呈现/显示给定的任何对象的名称。考虑到这一点。您需要有一个名为 Wish 的类,并实现 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,
    }
}

愿望 TableView Controller :

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

结果:

enter image description here

关于ios - Swift - 初始化 UITableViewCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58985344/

相关文章:

ios - 我无法在我的 UITableView 中加载超过 40 个 UITableViewCells

ios - 无法修改 SKSpriteNodes 的 objectAtIndex NSMutableArray

objective-c - 如何更改 MFMailComposeViewController 中的按钮?

ios - 如何知道我的应用程序正在使用哪个分发/开发配置文件

ios - 多重过滤 Swift 的 boolean 语句

ios - 如何以数组形式访问 firebase 目录名称?

ios - SWRevealViewController 和 TableView - 滑动删除不工作

ios - 将 AVFoundation 与高分辨率视频一起使用时出现 -11855 错误代码。我应该使用什么分辨率?

ios - 通过 Swift 中的触摸检测应用程序何时处于事件状态或非事件状态

ios - TableView 或 TableView 单元格宽度不正确