3

При попытке загрузить пользовательский UITableViewCell из ниба, я не могу заставить ячейку отображаться только в том случае, если я установил идентификатор повторного использования в ячейку прототипа основной раскадровки. Если для этого параметра значение пустое, я получаю предупреждение от xcode, однако код работает правильно. У меня есть аналогичный код в нескольких других контроллерах, но это единственный, кто представляет эту проблему.Установка reuseIdentifier на раскадровке приводит к тому, что пользовательский UITableViewCell не отображается.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
static NSString *cellIdentifier = @"AssetsTableCell"; 
AssetsTableCell *assetTableCell = (AssetsTableCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

int rightIndex = (indexPath.row * 4); 
int rightMiddleIndex = (indexPath.row * 4) + 1; 
int leftMiddleIndex = (indexPath.row * 4) + 2; 
int leftIndex = (indexPath.row * 4) + 3; 

if (assetTableCell == nil) { 
    NSArray *topLevelObjects; 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) { 
     topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AssetsTableCell_iPhone" owner:nil options:nil]; 
     assetTableCell = (AssetsTableCell *)[topLevelObjects objectAtIndex:0]; 
     [assetTableCell setDelegate:self]; 

    } 

    // Need to collect the badge details in groups of 4.  
    @try { 
     NSArray *rowArray = [NSArray arrayWithObjects:[remoteContainers objectAtIndex:rightIndex], 
                 [remoteContainers objectAtIndex:rightMiddleIndex], 
                 [remoteContainers objectAtIndex:leftMiddleIndex], 
                 [remoteContainers objectAtIndex:leftIndex], 
                 nil]; 

     NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys: 
            [rowArray objectAtIndex:0], @"LEFT_CONTAINER", 
            [rowArray objectAtIndex:1], @"LEFT_MIDDLE_CONTAINER", 
            [rowArray objectAtIndex:2], @"RIGHT_MIDDLE_CONTAINER", 
            [rowArray objectAtIndex:3], @"RIGHT_CONTAINER", nil]; 
     [assetTableCell populateCellData:cellData]; 
    } 
    @catch (NSException *exception) { 
     DLog(@"Exceeds boundary of remoteContainers array by %d.", ([remoteContainers count] % 4)); 
     int remainder = [remoteContainers count] % 4; 

     switch (remainder) { 
      case 1: { 
       // Check to see the number of remaining containers left in the rest of the array. This dictates the way the final row must be constructed. 
       if ([remoteContainers objectAtIndex:currentStartRangeIndex]) { 
        NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", @"EMPTY_CELL", @"LEFT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil]; 
        [assetTableCell populateCellData:cellData];     
       }      
      } 
       break; 
      case 2: { 
       // There are two elements at the end of the array. 
       NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], @"LEFT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil]; 
       [assetTableCell populateCellData:cellData]; 
      } 
       break; 
      case 3: { 
       // There are three elements at the end of the array.      
       NSDictionary *cellData = [NSDictionary dictionaryWithObjectsAndKeys:[remoteContainers objectAtIndex:rightIndex], @"LEFT_CONTAINER", [remoteContainers objectAtIndex:rightMiddleIndex], @"LEFT_MIDDLE_CONTAINER", [remoteContainers objectAtIndex:leftMiddleIndex], @"RIGHT_MIDDLE_CONTAINER", @"EMPTY_CELL", @"RIGHT_CONTAINER", nil]; 
       [assetTableCell populateCellData:cellData];      
      } 
       break; 
      default: { 
       DLog(@"Thought the array had more elements, however was unable to determine the number of elements remaining in array."); 

      } 
       break; 
     } 
    } 
} 
return assetTableCell; 

}

ответ

2

Похоже настроить каждую ячейку только в том случае, когда Tableview не DEQUEUE клетки для вас. Во-первых, повторное использование вашей ячейки, вероятно, нарушено. Так что это одна проблема.

А затем в случае раскадровки, если идентификатор ячейки установлен правильно, тогда [tableview dequeueReusableCellWithIdentifier:] всегда будет возвращать ячейку для вас. Таким образом, assetTableCell не будет равен нулю, даже в первый раз.

Непонятно, в чем состоит вся ваша логика, но вы должны иметь возможность правильно настроить содержимое своей ячейки в случае, когда assetTableCell не равен нулю. (либо потому, что ячейка перерабатывается, либо потому, что ячейка пришла из кеша раскадровки)

+0

Можете ли вы уточнить второй пункт? Почему ячейка таблицы не была бы равна нулю даже в случае, когда ранее не было выделено? – Kyle

+0

Поскольку в раскадровках, если в очереди на рециркуляцию нет ячеек, новый будет автоматически создан для вас из прототипа в раскадровке. –

+0

А, это была проблема. Так как ячейка никогда не была ноль, она никогда не загружала нить из пучка. Благодарю. – Kyle

Смежные вопросы