【问题标题】:Why am i getting a stacked text label in table view swift为什么我在表格视图中快速获得堆叠的文本标签
【发布时间】:2021-01-03 04:55:42
【问题描述】:

为什么我会收到这样堆积的文字? 我知道我可以从检查员那里删除“lbl”文本,但我不相信我应该得到这样的东西。 我错过了什么? 提前谢谢你

这是我的代码:

class ViewController: UITableViewController {

let name = [
"a", "b", "c", "d", "e", "f"
]

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    // Do any additional setup after loading the view.
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return name.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "bodyCell", for: indexPath)
    cell.textLabel?.text = name[indexPath.row]
    return cell
}

}

【问题讨论】:

  • 您的单元格中必须添加其他标签。

标签: ios swift uitableview tableview


【解决方案1】:

所以我只是想通了我自己,

我需要在dequeueReusableCell 之后进行转换 所以它看起来像这样:

let cell = tableView.dequeueReusableCell(withIdentifier: "bodyCell", for: indexPath) as! OutletClass

所以 OutletClass 是我连接标签的类。 好吧,至少我是这样。

【讨论】:

    【解决方案2】:

    尝试以编程方式进行:

    class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    let tableView = UITableView()
    let name = [
    "a", "b", "c", "d", "e", "f"
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "bodyCell")
    
        // add your tableView and add constraint
        view.addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
        tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return name.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "bodyCell", for: indexPath)
        cell.textLabel?.text = name[indexPath.row]
        return cell
      }
    }
    

    【讨论】:

    • 它是UITableViewController。您不需要设置dataSource,如果cellstoryboard 本身内,则也不需要注册它。从屏幕截图中可以看出,代码运行良好,文本设置在单元格的 textLabel 上。
    • 嗯,首先这不是我的问题。其次,它已在问题中指定class ViewController: UITableViewController {
    • 好的,所以我仍然无法弄清楚,这真是太糟糕了,我通常可以替换情节提要中的任何文本标签。我已经仔细检查过,那里只有 1 个标签。我的猜测是在这部分:cell.textLabel?.text = name[indexPath.row] 可能会创建另一个文本标签?我已经连接了另一个班级的插座,但是我无法通过cell.访问它
    • 嗨,Aldo,你是意大利人吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-15
    • 2015-02-19
    相关资源
    最近更新 更多