How can I check if a reuse identifier has been registered with a UITableView already?

纵饮孤独 提交于 2019-12-10 12:51:40

问题


In iOS apps, we have to register nib files with our table view before we can use UITableView#dequeueReusableCellWithIdentifier.

Example:

static NSString *myReuseIdentifier = @"MyReuseIdentifier";
UINib *cellNib = [UINib nibWithNibName:myReuseIdentifier bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:myReuseIdentifier];

Is there a way to check if a Nib has already been registered with a UITableView?

I have a custom cell that I use in various tables across several controllers in my app. I'd like to move some of the code to a macro. Something like

-(CustomCell *)customCell:(UITableView *)tableView
{
    static NSString *reuseIdentifier = @"MyReuseIdentifier";
    if (![table hasAlreadyRegisteredNib:reuseIdentifier]){
       UINib *cellNib = [UINib nibWithNibName:reuseIdentifier bundle:nil];
       [self.tableView registerNib:cellNib forCellReuseIdentifier:reuseIdentifier];     
    }
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    return cell;
}

回答1:


I am not sure if it that what you intend, but

-dequeueReusableCellWithIdentifier:

returns nil if the cell is not ready to reuse. Otherwise, it returns the cell, so you can simply try.




回答2:


I believe the point of registerNib:forCellReuseIdentifier: is to reduce boilerplate code. Could you simply call this once in your viewDidLoad method?



来源:https://stackoverflow.com/questions/9829058/how-can-i-check-if-a-reuse-identifier-has-been-registered-with-a-uitableview-alr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!