我的自定义UITableViewCell表格单元格显示为空项目列表

4

基本上,我有一个在nib文件中创建的自定义UITableViewCell。其中包含一些标签等元素,我希望能够通过编程进行编辑。我将我的nib文件命名为“post”,并将其文件所有者设置为“post”,并在文件所有者下将其类设置为“post”。我将我的nib中的所有标签都链接到了这个类:

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

我的xib文件的图片:

在这里输入图片描述

因此,我的xib文件中的所有内容都与我的post类链接,除了实际单元格本身,因为我被告知必须将其与“视图”链接,但我不知道如何进行链接(我尝试将其链接到backgroundView,但这没有解决我的问题)。我还尝试过将实际单元格对象赋予post类,但这并没有解决我的问题。

然后,在我的控制器中,我有以下代码:

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

}

然而,当我运行我的应用程序时,只会得到以下结果: enter image description here 我知道我遇到的问题与自定义单元格有关,因为之前它曾经起作用过。当我将文件所有者设置为我的控制器时,而不是尝试现在正在做的事情,即尝试创建UITableViewCell的子类。我想做的就是能够创建一个自定义单元格,并在其上放置多个不同的标签,然后能够以编程方式编辑单元格中的文本。
如果有帮助的话,这里是我的所有源文件和我的nib文件: https://dl.dropboxusercontent.com/u/7822837/source.zip

1
您的控制器没有提供参考输出。 - yen
3个回答

10

你需要在UITableView中注册Nib以便能使用cell重用标识符,类似于下面的代码:

- (void)viewDidLoad {
   [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"];
}

Swift:

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.registerNib(UINib(nibName: "post", bundle: NSBundle.mainBundle()), forCellReuseIdentifier: "post")
}

我不确定那是我的问题,但我应该在我的自定义类中覆盖那个方法,以确保我的TableView重用单元格。谢谢! - David Zorychta
1
只是想指出这段代码不正确,因为您试图在UINib的位置传递一个NSString。正确的代码是:[self.tableView registerNib:[UINib nibWithNibName:@"post" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"post"]; - Will
在花费了两天的时间寻找同样的问题后,这行代码解决了它。谢谢。我不明白为什么所有我读过的例子中都没有它? - Guillaume Besse

5

你需要完成两件事情

在xib文件中

断开文件所有者的所有输出口,并将输出口连接到你自定义的单元格对象

enter image description here

在代码中

从nib加载单元格

if (cell == nil) {

    //cell = [[post alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    cell = [[[NSBundle mainBundle]loadNibNamed:@"post" owner:self options:nil]objectAtIndex:0];
}

谢谢,那个很好用!在文件所有者和单元格之间建立连接的区别是什么? - David Zorychta
太棒了...非常感谢...我也犯了同样的错误...从文件所有者连接。 - Saheb Singh

0

给你的ViewController添加引用 Outlet,你没有为你的ViewController添加引用 Outlet。


你的意思是我应该在我的控制器上创建一个IBOutlet,然后直接将其连接到我的单元格上?这样可以工作,但是我无法编辑我添加到单元格中的多个标签,因为我没有任何要连接它们的东西。 - David Zorychta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接