我的自定义 UITableViewCell 表格单元格显示为项目的空白列表
Posted
技术标签:
【中文标题】我的自定义 UITableViewCell 表格单元格显示为项目的空白列表【英文标题】:My custom UITableViewCell table cell shows up as a blank list of items 【发布时间】:2013-04-26 04:38:31 【问题描述】:基本上,我在我的 nib 文件中创建了一个自定义 UITableViewCell。其中我有一些标签和诸如此类的东西,我希望能够以编程方式进行编辑。我将我的 nib 文件命名为“post”,并将其文件所有者设为“post”,并将其类设置为文件所有者下的“post”。我将笔尖中的所有标签都链接到了这个类:
post.h:
#import <Foundation/Foundation.h>
@interface post : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *title;
@property (nonatomic, weak) IBOutlet UILabel *authorComments;
@property (nonatomic, weak) IBOutlet UILabel *time;
@end
post.m:
#import "post.h"
@implementation post
@synthesize title = _title;
@synthesize authorComments = _authorComments;
@synthesize time = _time;
@end
我的 nib 文件的图片:
所以现在我的 nib 文件中的所有内容都与我的帖子类 except 链接到实际单元格本身,因为我被告知我必须将其与“视图”链接,但我不确定如何(我尝试将它链接到 backgroundView 但这并没有解决我的问题)。我也尝试过给实际的单元格对象提供类帖子,但它不能解决我的问题。
然后在我的控制器中我有以下代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
// this is the identifier that I gave the table cell within my nib
static NSString *simpleTableIdentifier = @"post";
// grabs me a cell to use
post *cell = (post *) [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
// sets the text on one of the labels within my cell to something else
cell.title.text = @"this is a test";
// returns my customized cell
return cell;
但是,当我运行我的应用程序时,我得到了这个:
我知道我遇到的问题与我的自定义单元格有关,因为我之前有它工作过,当我将我的笔尖上的文件所有者设置为我的控制器时,而不是尝试我现在正在做的事情正在尝试创建 UITableViewCell 的子类。我要做的就是能够创建一个带有多个不同标签的自定义单元格,然后能够以编程方式编辑单元格上的文本。
如果有帮助,这里是我所有的源文件和我的 nib 文件:
https://dl.dropboxusercontent.com/u/7822837/source.zip
【问题讨论】:
您的控制器没有提供参考插座。 【参考方案1】:您必须在您的 UITableView 中注册 Nib:forCellReuseIdentifier:,如下所示:
- (void)viewDidLoad
[super viewDidLoad];
[self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"];
斯威夫特:
override func viewDidLoad()
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
【讨论】:
我不确定这是我的问题,但我可能应该在我的自定义类中重写该方法,以确保我的 TableView 重用单元格。谢谢! 只是想指出这段代码不正确,因为您试图传递一个 NSString 来代替 UINib。正确的代码是:[self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"]; 在花了 2 天时间搜索同样的问题后,这行代码修复了它。谢谢你。我不明白为什么在我阅读的所有示例中都没有它?【参考方案2】:你必须做两件事
在 xib 文件中
断开文件所有者的所有出口并将出口连接到您的自定义单元对象
在代码中
从笔尖加载单元格
if (cell == nil)
//cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
cell = [[[NSBundle mainBundle]loadNibNamed:@"post" owner:self options:nil]objectAtIndex:0];
【讨论】:
谢谢,效果很好!在文件所有者和单元格之间建立连接有什么区别? Cooolll...非常感谢...我犯了同样的错误...从文件所有者连接。【参考方案3】:给你的ViewController
提供引用出口,你没有给你的ViewController
一个引用出口。
【讨论】:
你的意思是我应该在我的控制器上创建一个 IBOutlet 并将它连接到我的单元格吗?这行得通,但是我无法编辑添加到单元格中的多个标签,因为我没有任何东西可以连接它们。以上是关于我的自定义 UITableViewCell 表格单元格显示为项目的空白列表的主要内容,如果未能解决你的问题,请参考以下文章
Swift 中的自定义 UITableViewCell 注册类