简体   繁体   English

UITableViewCell不显示内容

[英]UITableViewCell doesn't show content

I am using Xcode 5, and developing an app targeted to iOS6/7. 我正在使用Xcode 5,并开发针对iOS6 / 7的应用程序。

In my storyboard, the "prototype cells", that refer to custom class "ObjListCell", derived from "UITableViewCell", contains a "Content View" that in turn contains a UIImageView and two UILabel elements. 在我的故事板中,“原型单元”,引用自定义类“ObjListCell”,派生自“UITableViewCell”,包含“内容视图”,后者又包含UIImageView和两个UILabel元素。 All three of them are connected to an IBOutlet in my ObjListCell class. 所有这三个都连接到我的ObjListCell类中的IBOutlet。

The table view controller refers to my custom class "ObjListViewController", derived from "UITableViewController". 表视图控制器引用我的自定义类“ObjListViewController”,派生自“UITableViewController”。 When this view controller is created, I have already retrieved the data, so in the viewDidLoad i call [self.tableView reloadData]; 当创建这个视图控制器时,我已经检索了数据,所以在viewDidLoad中我调用[self.tableView reloadData]; right away. 马上。 But before doing so, i register the cell class: [self.tableView registerClass: [ObjListCell class] forCellReuseIdentifier:@"objTableCell"]; 但在这之前,我注册了单元类: [self.tableView registerClass: [ObjListCell class] forCellReuseIdentifier:@"objTableCell"]; and "objTableCell" is set in the storyboard as the identifier for the cells. 并且在故事板中将“objTableCell”设置为单元格的标识符。

Method numberOfRows in section returns 14, and I actually see a list with 14 empty rows on my iPhone. 部分中的方法numberOfRows返回14,我实际上在iPhone上看到一个包含14个空行的列表。 As I can see from the logs, method cellForRowAtIndexPath is actually called, and method dequeueReusableCellWithIdentifier returns a non-nil cell. 正如我从日志中看到的那样,实际上调用了方法cellForRowAtIndexPath,并且方法dequeueReusableCellWithIdentifier返回一个非零单元格。 So I set the icon and label content: 所以我设置了图标和标签内容:

cell.mCellIcon.image = icon;
cell.mCellTitle.text = title;
cell.mCellExtra.text = extra;

I am sure the icon and text are OK, because I log them just before assigning them. 我确定图标和文本都没问题,因为我在分配它们之前就将它们记录下来。 But nevertheless, my table cells appear empty. 但是,我的桌子细胞显得空白。

Can anyone suggest where the problem could be? 任何人都可以建议问题出在哪里?

This is the whole code for cellForRowAtIndexPath: 这是cellForRowAtIndexPath的完整代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"objTableCell";
    ObjListCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    if (cell == nil)
        NSLog(@"ObjListViewController ERROR: Cell is nil");

    // Retrieve obj for this cell
    Obj* obj = [mObjs objectAtIndex:indexPath.row];
    if (obj == nil)
        NSLog(@"ObjListViewController ERROR: Obj not found for cell; indexPath = %i", indexPath.row);
    NSLog(@"ObjListViewController DEBUG: title = %@, extra = %@", obj.mTitle, obj.mExtra);

    // Set cell icon
    UIImage *icon = [mIcons iconForId:obj.mId andSize:@"big" withDefault:@"defaultIcon"];
    if (icon == nil)
        NSLog(@"ObjListViewController ERROR: Icon not found for cell; indexPath = %i", indexPath.row);
    cell.mCellIcon.image = icon;

    // Set cell title and extra
    cell.mCellTitle.text = obj.mTitle;
    if (obj.hasExtra)
        cell.mCellExtra.text = obj.mExtra;
    NSLog(@"ObjListViewController DEBUG: Inspecting cell content; icon = %@, title = %@, extra = %@",
     (cell.mCellIcon.image != nil ? @"OK" : @"nil"), cell.mCellTitle.text, cell.mCellExtra.text);

    // Standard accessory symbol
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    return cell;
}

And this is a excerpt of the log: 这是日志的摘录:

2014-02-16 15:21:06.665 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: title = Valle d'Aosta, extra = 
2014-02-16 15:21:06.668 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: Inspecting cell content; icon = nil, title = (null), extra = (null)
2014-02-16 15:21:06.685 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: title = Provincia di Belluno, extra = 
2014-02-16 15:21:06.690 trekking-etc-viewer[2671:60b] ObjListViewController DEBUG: Inspecting cell content; icon = nil, title = (null), extra = (null)

None of the "ERROR" log lines appear in the log. 日志中不显示“ERROR”日志行。

I would suggest placing [self.tableView reloadData] in viewWillAppear: as the tableView may not be available yet in viewDidload:. 我建议在viewWillAppear中放置[self.tableView reloadData]:因为tableView在viewDidload中可能还不可用:

Also, if I am having problems like this, I will give my objects borders to see if they are actually placed where I expect them to be: 此外,如果我遇到这样的问题,我会给我的对象边框,看看它们是否实际放置在我预期的位置:

label.layer.borderWidth = 1.0f;
label.layer.borderColor = [UIColor greenColor].CGColor;

How about trying the following in cellForRowAtIndexPath: 如何在cellForRowAtIndexPath中尝试以下内容:

static NSString *CellIdentifier = @"objTableCell";

ObjListCell *cell = (ObjListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell)
{
    cell = [[DDTripCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

I am definitely unable to make it work using a storyboard, despite the many suggestions and attempts. 尽管有许多建议和尝试,我绝对无法使用故事板使其工作。 For sure I am missing some little tricky detail somewhere, but tools don't provide any useful help on the matter. 当然,我在某处遗漏了一些棘手的细节,但工具并没有提供任何有用的帮助。 So I reverted to NIBs, and so this is my answer for the moment. 所以我回到了NIB,所以这是我目前的答案。

I would like just to summarize the few things I have taken care not to miss in defining and using the cell's nib: 我想总结一下我在定义和使用单元格的笔尖时不要错过的一些事项:

  • In the NIB, set the table view controller as the file's owner 在NIB中,将表视图控制器设置为文件的所有者
  • In the NIB, set the cell identifier to the proper value (objListCell, for me) 在NIB中,将单元标识符设置为正确的值(objListCell,对我来说)
  • In the NIB, connect each UI element to an IBOutlet in the cell's .h file 在NIB中,将每个UI元素连接到单元格的.h文件中的IBOutlet
  • In the cell's .m file, perhaps synthesise the outlets, but if you don't, it works all the same 在单元格的.m文件中,也许可以合成出口,但如果不这样做,它的工作原理完全相同
  • In viewWillAppear of the table's .m file, register the NIB (see details below) 在表的.m文件的viewWillAppear中,注册NIB(请参阅下面的详细信息)
  • In cellForRowAtIndexPath of the table's .m file, dequeue your cell (see details below) 在表的.m文件的cellForRowAtIndexPath中,将您的单元格出列(请参阅下面的详细信息)

Registering the nib: 注册笔尖:

UINib* cellNib = [UINib nibWithNibName:@"ObjListCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:@"objListCell"];

Dequeueing the cell: 将单元格出列:

static NSString *cellIdentifier = @"objListCell";
ObjListCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

All the rest is just as usual. 其余的都和往常一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM