故事板中的自定义UITableViewCell无法加载

时间:2014-08-09 19:54:00

标签: ios uitableview ios7.1

我面临一个奇怪的问题。我有一个带有UITableViewController的iOS 7.1故事板应用程序,使用Xcode 5.我创建了一个UITableViewCell的子类,并将其设置为IB中的单元格类,并添加了一个单元格标识符。样式也设置为Custom。尽管如此,我还没有对这个子类做过任何修改。它只包含您在创建新类时获得的模板代码。

在表视图控制器中,我有这个代码。只需设置数据源和委托方法。

#import "TableViewController.h"
#import "TextFieldTableViewCell.h"

@interface TableViewController ()

@end

@implementation TableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    TextFieldTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.textLabel.text = @"hello world";

    return cell;
}

cellForRowAtIndexPath中,我只需将值设置为单元格的textLabel属性。即使这样也不会显示出来。我仔细检查了标识符,样式和所有设置的所有内容。

我不知道为什么这不起作用。我还查看了一些关于这个主题的在线教程,如this一个,但我已经掌握了所有内容。

任何人都可以告诉我,我是否遗漏了什么?

我已将测试项目上传到我的Dropbox here,如果您想快速浏览一下。

1 个答案:

答案 0 :(得分:2)

发现它!你已经覆盖了TextFieldTableViewCell类的layoutSubviews,并且没有在其中写入任何内容。

只需删除layoutSubviews(暂时),它就能正常工作。

更新:更好的解决方案;

- (void) layoutSubviews
{
    [super layoutSubviews];
}