单个故事板swift中的多个表视图

时间:2014-10-23 21:32:42

标签: ios iphone uitableview swift

我试图在一个视图控制器中有2个tableview,其中一个tableview是静态的 另一种动态。

我的视图控制器设置如此 enter image description here

上半部分是静态的tableview。

我为两个tableView创建了ibOutlets,但我似乎无法自定义表。

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

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

    //tableview2 is the dynamic tableView.
    if (tableView == self.tableView2){
        print("Tableview2")

    }
    else{
        println("HELLLO")
    }




    return cell
}

我在 - [UITableView dequeueReusableCellWithIdentifier:forIndexPath:],/ SourceCache / UIKit_Sim / UIKit-3318.16.14 / UITableView.m:6116中收到错误 *断言失败 2014-10-23 22:31:52.246 Recipe app [2857:504809] * 由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'无法使用标识符cell2对单元格出列 - 必须注册一个笔尖或一个标识符的类或连接故事板中的原型单元格。

2 个答案:

答案 0 :(得分:1)

正如错误所说,你必须为标识符注册一个笔尖或一个类,或者在故事板中连接一个原型单元"。

在viewDidLoad或awakeFromNib上执行此操作。

self.tableView.registerClass(UITableViewCell.classForCoder(), forCellReuseIdentifier:"cell2")

或者你必须在故事板中制作原型单元并给它" cell2"标识符

enter image description here

答案 1 :(得分:0)

假设您已为动态表格提供了原型单元格reuseRedentifier" cell2",请移动此行:

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

if ...子句或else ...子句(以动态表为准)。如果tableView是静态表,则会抛出错误。

相关问题