Question

If I try to add an opened UIDocument into an array I get nothing out. This might be how it is supposed to work, I'm not sure.

- (void)loadDocAtURL:(NSURL *)fileURL withClassName:(NSString *)className {


    id doc = [[NSClassFromString(className) alloc] initWithFileURL:fileURL];

    [doc openWithCompletionHandler:^(BOOL success) {


        if (!success) {
            NSLog(@"Failed to open %@", fileURL);
            return;
        }

        NSLog(@"I'm a doc. My class is %@, my title is %@", NSStringFromClass([doc class]), [doc title]);

        dispatch_async(dispatch_get_main_queue(), ^{
        //[self addOrUpdateEntryWithURL:fileURL metadata:metadata state:state version:version];
        [_tableList addObject:doc];
        [self.tableView reloadData];
        NSLog(@"%d", _tableList.count);
        });

    }];

};

That NSLog returns: I'm a doc. My class is Song, my title is A New Song

So far so good.

[self.tableView reloadData], reloads the table correctly and the correct number of cells are displayed, only they are empty, this is why:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    Song *theSong = [tableList objectAtIndex:indexPath.row];
    NSLog(@"I am a %@", NSStringFromClass([theSong class]));
    UILabel *titleLabel = (UILabel *)[cell viewWithTag:0];
    UILabel *lyricLabel = (UILabel *)[cell viewWithTag:1];
    NSLog(@"I'm in the table view. My title is %@", [theSong lyric]);
    titleLabel.text = theSong.title;
    lyricLabel.text = theSong.lyric;
    [theSong closeWithCompletionHandler:^(BOOL success) {

        // Check status
        if (!success) {
            NSLog(@"Failed to close %@", theSong.fileURL);
            // Continue anyway...
        }

        }];
    return cell;
}

Those two NSLogs output: I am a (null) and I'm in the table view. My title is (null)

When I breakpoint and look at theSong after its grabbed, it is completely empty.

This is leading me to think that you cannot open a UIDocument, stick it in an array and pull it back out of the array. Am I correct?

Was it helpful?

Solution

If the song is nil, then almost certainly the array is nil. I notice you refer to it both as _tableList and as tableList. My guess is those are not the same pointer.

Try using _tableList in cellForRowAtIndexPath. Log it to confirm that it's non-nil.

You can disprove your theory about the array by logging [_tableList lastObject] right after you add it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top