如何检查重用标识符是否已在 UITableView 中注册? [英] How can I check if a reuse identifier has been registered with a UITableView already?

查看:75
本文介绍了如何检查重用标识符是否已在 UITableView 中注册?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 应用中,我们必须在我们的 table view 中注册 nib 文件,然后才能使用 UITableView#dequeueReusableCellWithIdentifier.

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

示例:

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

有没有办法检查 Nib 是否已经注册到 UITableView?

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;
}

推荐答案

我不确定这是否是您想要的,但是

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

-dequeueReusableCellWithIdentifier:

返回 nil 如果单元格没有准备好重用.否则,它会返回单元格,因此您可以简单地尝试.

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

这篇关于如何检查重用标识符是否已在 UITableView 中注册?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆