Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1078
长情又很酷
长情又很酷 2020-11-22 10:10

So I was making an rss reader for my school and finished the code. I ran the test and it gave me that error. Here is the code it\'s referring to:

- (UITableV         


        
22条回答
  •  名媛妹妹
    2020-11-22 11:06

    Swift 2.0 solution:

    You need to go into your Attribute Inspector and add a name for your cells Identifier:

    Then you need to make your identifier match with your dequeue like this:

    let cell2 = tableView.dequeueReusableCellWithIdentifier("ButtonCell", forIndexPath: indexPath) as! ButtonCell
    

    Alternatively

    If you're working with a nib you may need to register your class in your cellForRowAtIndexPath:

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {       
    
        tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "SwitchCell")
    
        // included for context            
        let cell = tableView.dequeueReusableCellWithIdentifier("SwitchCell", forIndexPath:indexPath) as! SwitchCell
    
        //... continue    
    }
    

    Apples's UITableView Class Reference says:

    Prior to dequeueing any cells, call this method or the registerNib:forCellReuseIdentifier: method to tell the table view how to create new cells. If a cell of the specified type is not currently in a reuse queue, the table view uses the provided information to create a new cell object automatically.

    If you previously registered a class or nib file with the same reuse identifier, the class you specify in the cellClass parameter replaces the old entry. You may specify nil for cellClass if you want to unregister the class from the specified reuse identifier.

    Here's the code from Apples Swift 2.0 framework:

    // Beginning in iOS 6, clients can register a nib or class for each cell.
    // If all reuse identifiers are registered, use the newer -dequeueReusableCellWithIdentifier:forIndexPath: to guarantee that a cell instance is returned.
    // Instances returned from the new dequeue method will also be properly sized when they are returned.
    
    @available(iOS 5.0, *)
    func registerNib(nib: UINib?, forCellReuseIdentifier identifier: String)
    
    @available(iOS 6.0, *)
    func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String)
    

提交回复
热议问题