使用Storyboard时,为什么我的实例变量为nil?

时间:2012-09-06 17:05:59

标签: objective-c xcode storyboard

我正在努力让LazyTableImages与Storyboard和ARC合作。我的项目编译并显示“Loading ...”和Placeholder.png图像。我的日志显示解析器正在检索大部分数据(艺术家,appName),但是缩略图图像(错误的URL)有问题。最大的问题似乎在这里(nodeCount的NSLog始终为零)。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    NSLog(@"RootViewController - tableView cellForRowAtIndexPath");

    // customize the appearance of table view cells
    static NSString *CellIdentifier = @"Cell";
    static NSString *PlaceholderCellIdentifier = @"PlaceholderCell";

    // add a placeholder cell while waiting on table data
    int nodeCount = [self.entries count];

    NSLog(@"RootViewController - nodeCount is %d",nodeCount);

    if (nodeCount == 0 && indexPath.row == 0)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier];
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                             
                                          reuseIdentifier:PlaceholderCellIdentifier];   
            cell.detailTextLabel.textAlignment = UITextAlignmentCenter;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        cell.detailTextLabel.text = @"Loading…";
        return cell;
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle                                    
                                      reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSLog(@"RootViewController - nodeCount2 is %d",nodeCount);

    // Leave cells empty if there's no data yet
    if (nodeCount > 0)
    {
        NSLog(@"RootViewController - nodeCount3 is %d",nodeCount); //this one never gets displayed

        // Set up the cell...
        AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];

        cell.textLabel.text = appRecord.appName;
        cell.detailTextLabel.text = appRecord.artist;

        // Only load cached images; defer new downloads until scrolling ends
        if (!appRecord.appIcon)
        {
            if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
            {
                [self startIconDownload:appRecord forIndexPath:indexPath];
            }
            // if a download is deferred or in progress, return a placeholder image
            cell.imageView.image = [UIImage imageNamed:@"Placeholder.png"];                
        }
        else
        {
            cell.imageView.image = appRecord.appIcon;
        }

        NSLog(@"RootViewController - nodeCount4 is %d",nodeCount);
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

问题是因为用于设置RootViewController的AppDelegate方法在使用NIB和Storyboard之间是不同的...因此它没有获得alloc。将以下内容添加到applicationDidLaunch解决了它:

rootViewController = [[RootViewController alloc] init]; 
self.window.rootViewController = self.rootViewController; 
[window addSubview:rootViewController.view]; 
[window makeKeyAndVisible];  

然后将条目分配给RootViewController,如下所示:

self.appRecords = [NSMutableArray array]; 
rootViewController.entries = self.appRecords; 

...所以如果rootViewController不存在,则条目不能存在......