2013-04-28 171 views
0

我需要調用一個方法的行被添加到表格視圖後,但和我想這iOS的自定義表格視圖單元格是空

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

回答

0

因爲重用機制。(如果你使用的cellForRowAtIndexPath獲得細胞,細胞可能無法創建或不可見的將返回null。的cellForRowAtIndexPath返回正確只電池(行&部分)是可見)

Xib的稱重傳感器可能會導致此問題。但如果你通過代碼創建單元格不會。

for(id currentObject in topLevelObjects) 
    { 
     if([currentObject isKindOfClass:[ACDownloadCell class]]) 
     { 
      cell = (ACDownloadCell *)currentObject; 
      break; 
     } 
    } 
    NSLog(@"cell=%@", cell); // will output null. 
} 
0

您爲重新使用您的單元格的代碼看起來看起來很直接和適當。我懷疑你的xib文件搞砸了,你沒有將你的單元格劃分爲ACDownloadCell

另外,我覺得你應該考慮升級到使用

registerNib:forCellReuseIdentifier: 

更多信息在此可以看出here

相關問題