【问题标题】:how to add a UItableview without storyboard? (UITableView error )如何在没有情节提要的情况下添加 UItableview? (UITableView 错误)
【发布时间】:2015-09-12 21:08:54
【问题描述】:

我使用swift添加了一个UITableview(没有故事板),但是发生了错误,我不知道为什么

错误是

2015-06-26 11:08:14.149 test[62289:4994907] *** 断言失败 -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], /SourceCache/UIKit_Sim/UIKit-3347.44/UITableView.m:6245

2015-06-26 11:08:14.152 test[62289:4994907] *** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 TextCell 的单元格出列 - 必须注册一个 nib 或一个标识符的类或连接故事板中的原型单元'

我不知道怎么解决

来源是:

import UIKit

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    let swiftBlogs = ["a","b","c","d"]

    let textCellIndetifier = "TextCell"

    var t = UITableView()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        t.frame = self.view.frame
        self.view.addSubview(t)
    
        t.delegate = self
        t.dataSource = self
    
    }

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

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

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = t.dequeueReusableCellWithIdentifier(textCellIndetifier, forIndexPath: indexPath) as! UITableViewCell
        let row = indexPath.row
        cell.textLabel?.text = swiftBlogs[row]
        return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        t.deselectRowAtIndexPath(indexPath, animated: true)
    
        let row = indexPath.row
        println(swiftBlogs[row])
    }

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


}

【问题讨论】:

标签: ios swift uitableview


【解决方案1】:

您需要注册您正在使用的单元格,因为您没有情节提要,所以您需要在 XIB 文件中创建一个 UITableViewCell 元素。

override func ViewDidLoad(){
  super.viewDidLoad()
  self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "TextCell")
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    相关资源
    最近更新 更多