Uitableview 具有Swift的CustomCell和具有原型单元的独立类

Uitableview 具有Swift的CustomCell和具有原型单元的独立类,uitableview,swift,Uitableview,Swift,全部, 我有一个示例项目,它是一个带有原型单元的tableview。 我正在尝试创建一个customCell,我已经创建了一个类,并将该类链接到原型单元 这是我的密码: import UIKit class CustomCell: UITableViewCell { @IBOutlet weak var SampleTextLabel: UILabel! } class ViewController: UIViewController, UITableViewDelegate,

全部,

我有一个示例项目,它是一个带有原型单元的tableview。 我正在尝试创建一个customCell,我已经创建了一个类,并将该类链接到原型单元

这是我的密码:

import UIKit

class CustomCell: UITableViewCell {


    @IBOutlet weak var SampleTextLabel: UILabel!

}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tableView: UITableView!
    var items: [String] = ["I", "Hate", "Swift"]

    override func viewDidLoad() {
        super.viewDidLoad()

    }

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

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.items.count

}

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

    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("CustomCell") as CustomCell


    //cell.textLabel?.text = self.items[indexPath.row]
    return cell
}
无论我做什么,我都无法访问cell.sampleTextLabel,我已经在出列行的末尾添加了as CustomCell。有人知道我为什么看不到标签吗

在cellForRowAtIndexPath中,祝您一切顺利。


@Rob-ah,你是对的,在op中没有提到。你仍然需要调用forIndexPath:在队列中,尽管正确?forIndexPath是首选的,应该使用,但在技术上不是必需的。你为你的细胞原型指定了基类吗?您确定您的单元原型已添加新标签到单元;b这个标签上的插座已经连接到自定义类别标签插座上了?另外,您是否为标签设置了约束条件?检查框架上的标签。您可以运行应用程序,点击暂停按钮,并在lldb提示下输入po[[UIWindow keyWindow]recursiveDescription]。这将显示所有视图的框架。
var cell: CustomCell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as CustomCell
cell.SampleTextLabel.text = self.items[indexPath.row]
return cell