【问题标题】:Swift - initialize UITableViewCellSwift - 初始化 UITableViewCell
【发布时间】:2019-11-21 23:36:49
【问题描述】:

我的问题很简单。我想要的只是在没有情节提要的情况下以编程方式设置UITableView

我创建了我的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
    }()

然后我也 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)
        })}

【问题讨论】:

  • let cell = tableView.dequeueReusableCell(withIdentifier: "whishCell") as! WhishCell
  • 对不起,你是什么意思?我没有在 Storyboard 中做任何事情
  • @Sh_Khan 明白了。现在它说我需要在super.init 中初始化self.whishText。但是我该怎么做呢?
  • 您需要将模型与视图分开。单元格不应具有其呈现的数据的属性。使用用于向单元格提供数据的模型。如果有必要,单元格可以有一个名为 wishTextLabel 的 UILabel,但不是字符串实例本身。请参阅下面的示例。

标签: ios swift uitableview initialization


【解决方案1】:

你在那里做一件非常错误的事情。您直接在单元格上设置数据(该变量名为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,
    }
}

WishTableViewController:

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

结果:

【讨论】:

  • 不知何故这对我不起作用。我实现了你的确切代码。我唯一不同的是我的TableView 已经在我的UIViewController 中,我只是让它出现/消失在. transofrm 中。
  • @DieGlueckswurst .transform? 您需要发布代码的相关位。特别是 tableview 代码,以及如何创建和调用 tabview 的表示。你肯定做错了你认为是正确的事情:) 我只能在你添加更多代码后才能知道。
  • 当然,我刚刚更新了我的问题 :) 抱歉,transform 实际上无关紧要。我只是用它来更改y-position,这样我就可以让视图出现和消失。但你可以忽略这一点
  • @DieGlueckswurst 我需要查看您的数据来自何处/如何将其传递给 tableview。同时发布WhishlistTableViewController的代码
  • WhishTableViewController 是您的确切代码 + 我在该文件中也有 class Whish。唯一不同的是我没有func showExampleViewTable,而是我复制了function 里面的内容,并将它放在ViewDidLoad() 里面我的UIViewController
猜你喜欢
  • 2021-08-02
  • 1970-01-01
  • 2011-08-19
  • 2021-01-22
  • 2021-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多