简体   繁体   English

iOS自定义表格视图单元格为空

[英]iOS custom table view cell is null

I need to call a method after a row is added to a table view, and am trying this 在将行添加到表视图后,我需要调用一个方法,并且正在尝试这样做

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];
}

however, when it logs cell , it logs (null) . 但是,在记录cell ,将记录为(null) Do you see any thing here that is incorrect? 您在这里看到任何不正确的东西吗? Here's my tableView:cellForRowAtIndexPath: 这是我的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;
}

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.) 因为使用了重用机制。(当您使用cellForRowAtIndexPath来获取单元格时,单元格可能不会创建或不可见,它将返回null。cellForRowAtIndexPath返回正确的是只有单元格(行和节)才可见。)

Load cell from xib may cause this problem. 来自xib的称重传感器可能会导致此问题。 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.
}

You code for re-using your cell looks looks very straightforward and appropriate. 您为重新使用单元格外观编写的代码看起来非常简单和适当。 I suspect your xib file is messed up and you don't have your cell appropriately classed as an ACDownloadCell . 我怀疑您的xib文件被弄乱了,并且您没有将您的单元格正确分类为ACDownloadCell

Also, I think you should consider upgrading to using 另外,我认为您应该考虑升级到使用

registerNib:forCellReuseIdentifier:

more info on this can be seen here 更多信息可以在这里看到

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

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