2016-10-27 1 views
0

Создание tableView по storyboard, и в методе tableView delegate: tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell происходит исключение:Создание Tableview по раскадровке использовать пользовательские tableViewCell образуют XIB получить отказ Assertion

*** Assertion failure in -[UITableView dequeueReusableCellWithIdentifier:forIndexPath:], 
    /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3599.6/UITableView.m:6593 

мой код находится ниже:

ViewController11

@IBOutlet weak var tableView: UITableView! // tableView 

// tableview delegate 
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell_id: String = "cell_id" 
    // this line below occurs exception: Thread 1:breakpoint 5.2 
    var cell: TableViewCell2 = tableView.dequeueReusableCell(withIdentifier: cell_id, for: indexPath) as! TableViewCell2 

    if cell == nil { 

     // 
    } 

    var title = dataSource[indexPath.row] 

    cell.titlelabel?.text = String(title) 


    return cell 
} 

и мой storyboard для ViewController11:

viewController11

и мой xib для TableViewCell2:

TableViewcell12

+2

увидеть этот раз http://stackoverflow.com/questions/12737860/assertion-failure-in-dequeuereusablecellwithidentifierforindexpath –

+1

перед тем извлечение из ячейки вам необходимо зарегистрировать его '' class' или nib' с помощью 'tableView'. – Adeel

ответ

0

Вы должны зарегистрировать Ваш UITableViewCell от XIb, прежде чем вы можете использовать его:

Swift 2:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.registerNib(UINib(nibName: "TableViewCell2", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "cell_id") 
} 

Swift 3:

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.tableView.register(UINib(nibName: "TableViewCell2", bundle: Bundle.main), forCellReuseIdentifier: "cell_id") 
} 
+0

в swift3 'NSBundle.mainBundle()' shold заменить на 'Bundle.main' – aircraft

+0

Добавлено решение Swift 3. // @самолет –

Смежные вопросы