簡體   English   中英

iOS Swift 2.0:使用未解決的標識符“UITableviewCell”

[英]iOS Swift 2.0: Use of unsolved identifier 'UITableviewCell'

我正在學習制作一個簡單應用程序的編碼教程,起初一切看起來和工作正常,但過了一段時間我遇到了一個錯誤說:

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

我無法對 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]

你應該出列 UITableViewCells。 像這樣的東西

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

您想重復使用單元格,而不是每次都創建一個新單元格。 這是執行此操作的首選方式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM