2013-10-14 5 views
0

Я создаю приложение, которое использует Core Data и UITableViewControllers, чтобы отобразить список MELs.Если инструкция для таблицы не работает, ячейки должным образом

Я сделал все, но не могу согласиться с проверкой UITableViewCell должен быть доступен для редактирования или нет. Вот скриншот моего приложения, которые должны помочь вам представить себе мою проблему:

enter image description here

Я проверяю, есть ли какие-либо chapter разделов. Если это правда, он отображает все в черном, если не цвет detailTextLabel изменен на красный. Но, как вы можете видеть, некоторые ячейки окрашены, даже если у них есть некоторые разделы. Как это возможно?

Вот мой tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Initializing Cell and filling it with info 
    Chapter *chapter = [self.MELs objectAtIndex:indexPath.row]; 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Number: %@ \t Sections: %lu", chapter.number, (unsigned long)[chapter.sections count]]; 
    cell.textLabel.text = [chapter.title capitalizedString]; 

    if ([chapter.sections count] == 0) { 
     [cell.detailTextLabel setTextColor:[UIColor redColor]]; 
    } 

    return cell; 
} 

ответ

3

, как клетки повторно вы должны сбросить TextColor по умолчанию, если условие не выполнено:

if ([chapter.sections count] == 0) { 
    [cell.detailTextLabel setTextColor:[UIColor redColor]]; 
} else { 
    [cell.detailTextLabel setTextColor:[UIColor blackColor]]; 
} 
+0

@Cojoj Вы можете пойти с Джонатаном Solution Или вы можете передать ноль вместо идентификатора ячейки; UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: nil forIndexPath: indexPath]; – Anuj

+0

@Anuj, используя nil как идентификатор, может вообще не привести к кешированию, я не думаю, что это нужно делать в таблицах. –

2

Это потому, что вы используете [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

[cell.detailTextLabel setTextColor:[UIColor redColor]]; сохраняется в ячейке многоразового использования. Если вы добавите дополнительные CellIdentifier для «красных» ячеек, проблема будет решена.

Дополнительно. вы должны проверить, не возвращается ли полученная ячейка для dequeueReusableCellWithIdentifier:forIndexPathnil;

Пример:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Initializing Cell and filling it with info 
    Chapter *chapter = [self.MELs objectAtIndex:indexPath.row]; 

    NSString *CellIdentifier = @"Cell"; 
    if ([chapter.sections count] == 0) { 
     CellIdentifier = @"Cell-red"; 
    } 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    if (cell == nil) { 
      // Cell properties 
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 

      if ([CellIdentifier isEqual:@"Cell-red"]) { 
       [cell.detailTextLabel setTextColor:[UIColor redColor]]; 
      } 
    } 

    cell.detailTextLabel.text = [NSString stringWithFormat:@"Number: %@ \t Sections: %lu", chapter.number, (unsigned long)[chapter.sections count]]; 
    cell.textLabel.text = [chapter.title capitalizedString]; 

    return cell; 
} 
2

Попробуйте это: -

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    if (indexPath.row % 2)// Change this according to your requirement 
    { 
     [cell.detailTextLabel setTextColor:[UIColor redColor]]; 
    } 
    else 
    { 
     [cell.detailTextLabel setTextColor:[UIColor blackColor]]; 
    } 
} 
Смежные вопросы