0

行がテーブルビューに追加された後にメソッドを呼び出す必要があり、これを試しています

if (_tableView) {
    [self.currentDownloads insertObject:url atIndex:0];
    NSLog(@"%@", _currentDownloads);
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    ACDownloadCell *cell = (ACDownloadCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [cell downloadFileAtURL:url];
}
else{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 499) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    [self.currentDownloads insertObject:url atIndex:0];
    NSLog(@"%@", _currentDownloads);
    [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
    ACDownloadCell *cell = (ACDownloadCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    NSLog(@"%@", cell);
    [cell downloadFileAtURL:url];
}

ただし、ログに記録する場合はログcellに記録し(null)ます。ここで何か間違っていることがわかりますか? これが私のtableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    ACDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"ACDownloadCell" owner:nil options:nil];

        for(id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[ACDownloadCell class]])
            {
                cell = (ACDownloadCell *)currentObject;
                break;
            }
        }
    }
    return cell;
}
4

2 に答える 2

0

Because reuse mechanism。(When you use cellForRowAtIndexPath to get the cell, cell may not create or not visible the will return null. cellForRowAtIndexPath return the correctly only the cell (row & section) is visible.)

Load cell from xib may cause this problem. but if you create cell by code will not.

    for(id currentObject in topLevelObjects)
    {
        if([currentObject isKindOfClass:[ACDownloadCell class]])
        {
            cell = (ACDownloadCell *)currentObject;
            break;
        }
    }
    NSLog(@"cell=%@", cell); // will output null.
}
于 2013-04-28T03:12:04.837 に答える
0

セルの外観を再利用するためのコードは、非常に簡単で適切に見えます。あなたの xib ファイルが台無しになっていて、セルがACDownloadCell.

また、使用へのアップグレードを検討する必要があると思います

registerNib:forCellReuseIdentifier:

これに関する詳細情報は、ここで見ることができます

于 2013-04-28T06:06:45.357 に答える