问题
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