【问题标题】:iOS Swift 2.0: Use of unsolved identifier 'UITableviewCell'iOS Swift 2.0:使用未解决的标识符“UITableviewCell”
【发布时间】:2016-06-27 23:54:49
【问题描述】:

我正在学习制作一个简单应用程序的编码教程,起初一切看起来都很好,但过了一段时间我遇到了一个错误:

use of unresolved identifier 'UITableViewCell'. 

本教程的代码在其视频中运行良好,我编写了完全相同的代码,但在我的计算机上出现了错误。我想这是不同版本的 Xcode 的问题。

这是我的代码:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet var tableView: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.tableView.dataSource = self
        self.tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 6
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        **let cell = UITableviewCell()**
        *//Where the error message is at. //*

        return cell

    }


}

错误信息在这一行:

let cell = UITableViewCell()

【问题讨论】:

  • 好吧,您发布的内容自相矛盾。你已经发布了 UITableviewCell() 和 UITableViewCell。只有一个是正确的,你的问题也有答案。

标签: ios swift error-handling uitableview


【解决方案1】:

我无法评论 Stefan Salatic 发布的答案,但您确实必须使用 dequeable 单元格,但要补充一点,您不应忘记将 main.storyboard 中的标识符设置为您用于创建 dequeable 单元格的 CellIdentifier。

let cell = tableView.dequeueReusableCellWithIdentifier("Identifier", forIndexPath: indexPath) as UITableViewCell

在情节提要中转到 TableViewController -> Attribute Inspector -> Identifier 并将其设置为:

Identifier

如果你有一个数据数组,你可以使用以下方法填充单元格:

cell!.textLabel?.text = data[indexPath.row]

【讨论】:

    【解决方案2】:

    你应该让 UITableViewCells 出队。像这样的

    let cell = tableView.dequeueReusableCellWithIdentifier("CellIdentifier", forIndexPath: indexPath) as UITableViewCell
    

    您希望重复使用单元格,而不是每次都创建一个新单元格。这是执行此操作的首选方式。

    【讨论】:

      猜你喜欢
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2015-05-13
      相关资源
      最近更新 更多