Assertion failure in dequeueReusableCellWithIdentifier:forIndexPath:

前端 未结 22 1077
长情又很酷
长情又很酷 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 10:57

    I give you the answer in both Objective C and Swift.Before that I want to say

    If we use the dequeueReusableCellWithIdentifier:forIndexPath:,we must register a class or nib file using the registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier: method before calling this method as Apple Documnetation Says

    So we add registerNib:forCellReuseIdentifier: or registerClass:forCellReuseIdentifier:

    Once we registered a class for the specified identifier and a new cell must be created, this method initializes the cell by calling its initWithStyle:reuseIdentifier: method. For nib-based cells, this method loads the cell object from the provided nib file. If an existing cell was available for reuse, this method calls the cell’s prepareForReuse method instead.

    in viewDidLoad method we should register the cell

    Objective C

    OPTION 1:

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    

    OPTION 2:

    [self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    

    in above code nibWithNibName:@"CustomCell" give your nib name instead of my nib name CustomCell

    SWIFT

    OPTION 1:

    tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    

    OPTION 2:

    tableView.registerNib(UINib(nibName: "NameInput", bundle: nil), forCellReuseIdentifier: "Cell") 
    

    in above code nibName:"NameInput" give your nib name

提交回复
热议问题