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

时间:2013-04-26 04:38:31

标签: ios objective-c nib

基本上我有一个我在我的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文件的图片:

enter image description here

所以现在我的nib文件中的所有内容都链接到我的帖子类除了以用于实际的单元格本身因为我被告知我必须将其与“视图”链接但我不知道如何(我尝试将它链接到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;

}

然而,当我运行我的应用程序时,我得到了这个:

enter image description here

我知道我遇到的问题与我的自定义单元格有关,因为我之前已将它工作,当我将笔尖上的文件所有者设置为我的控制器时,而不是尝试我现在​​正在做的事情正在尝试创建UITableViewCell的子类。我想要做的就是能够在其上创建一个包含多个不同标签的自定义单元格,然后能够以编程方式编辑单元格上的文本。

如果有帮助,这里是我的所有源文件和我的nib文件:

https://dl.dropboxusercontent.com/u/7822837/source.zip

3 个答案:

答案 0 :(得分:9)

你必须在你的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")
}

答案 1 :(得分:5)

你必须做两件事

在xib文件中

断开文件所有者的所有插座并将插座连接到自定义单元格对象

enter image description here

代码

从笔尖加载单元格

if (cell == nil) {

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

答案 2 :(得分:0)

将引用插座提供给ViewController,您没有为ViewController提供引用插座。

相关问题