1

我需要在 UITableView 中加载自定义单元格。我创建了一个名为“CustomTableViewCell”的自定义 UITableViewCell 子类。如图所示,我已将 UITabelViewCell 添加到 tableview(使用拖放)。然后在文件检查器中,我将该 UITabelViewCell 的类设置为“CustomTableViewCell”。这是我的代码:

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var items = String[]()


    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["Hi","Hello","How"]
        self.tableView.registerClass(CustomTableViewCell.self, forCellReuseIdentifier: "CusTomCell")
        // Do any additional setup after loading the view, typically from a nib.
    }

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

   func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
    var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell") as? CustomTableViewCell
    if !cell
    {
        cell = CustomTableViewCell(style: UITableViewCellStyle.Subtitle,
            reuseIdentifier: "CusTomCell")
    }
    println("cell \(cell)")
   // cell.?.labelTitle.text = items[indexPath.row]
    cell!.labelTitle.text = "some text"
    return cell
}





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


}

在此处输入图像描述

当我运行我的代码时,我收到以下错误:“致命错误:无法打开 Optional.None”,如图所示。

4

3 回答 3

4

嗨最后我找到了我的问题的解决方案..请检查我的代码..它对我有用..

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    @IBOutlet var tableView : UITableView

    var items = String[]()

    override func viewDidLoad() {
        super.viewDidLoad()
        items = ["Hi","Hello","How"]
        // Do any additional setup after loading the view, typically from a nib.
    }

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

    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
        var cell:CustomTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("CusTomCell", forIndexPath: indexPath) as? CustomTableViewCell
        cell!.labelTitle.text = "some text"
        return cell
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
于 2014-06-09T04:50:24.600 回答
2

第 1 步:在 viewDidLoad() 中注册 nib,如下所示

var xib : UINib = UINib (nibName: "WeatherTableViewCell", bundle: nil)
self.tableView.registerNib(xib, forCellReuseIdentifier: "Cell")

第 2 步:在 cellForRowIndexPath 中添加以下内容

var cell:WeatherTableViewCell? = self.tableView.dequeueReusableCellWithIdentifier("Cell") as? WeatherTableViewCell
cell!.weatherName.text = "weather" 
return cell
于 2014-06-23T11:46:29.893 回答
0

self.tableView.dequeueReusableCellWithIdentifier 只会在之前创建一个单元格时返回一个单元格。如果它是 nil,您必须进行 nil 检查并创建一个新单元格。在你的情况下,它会在 cell.labelTitle.text = 上出错,因为 cell 可能在那里为零。

于 2014-06-07T09:19:22.040 回答