2013-05-21 2 views
1

У меня есть TableView со статическими ячейками с заголовком, который имеет 4 ячейки, одна ячейка посередине несет UIWebView и в качестве нижнего колонтитула раздел с комментариями. У каждого комментария есть своя ячейка. Моя проблема в том, что нижний колонтитул имеет 4 или более комментариев, четвертая ячейка в нижнем колонтитуле несет тот же UIWebView, что и ячейка посередине.TableView перемешивает ячейки

Мой cellForRowAtIndexPath выглядит следующим образом:

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    // Get cell 
    static NSString *CellIdentifier = @"CellA"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    UITableViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (contentCell == nil) { 
     contentCell = [[CustomDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 



    // Display 
    cell.textLabel.textColor = [UIColor blackColor]; 
    cell.textLabel.font = [UIFont systemFontOfSize:15]; 
    if (item) { 

     // Item Info 
     NSString *itemTitle = item.title ? [item.title stringByConvertingHTMLToPlainText] : @"[No Title]"; 

     // Display 
     switch (indexPath.section) { 

      case SectionHeader: { 

       // Header 
       switch (indexPath.row) { 

        case SectionHeaderTitle: 
         cell.textLabel.font = [UIFont boldSystemFontOfSize:15]; 
         cell.textLabel.text = itemTitle; 
         cell.textLabel.numberOfLines = 0; // Multiline 
         break; 
        case SectionHeaderDate: 
         cell.textLabel.text = dateString ? dateString : @"[Kein Datum]"; 
         cell.imageView.image = nil; 
         break; 
        case SectionHeaderSharerFacebook: 
         cell.textLabel.text = @"Share on Facebook"; 
         cell.imageView.image = [UIImage imageNamed:@"f_logo.png"]; 
         break; 
        case SectionHeaderSharerTwitter: 
         cell.textLabel.text = @"Share on Twitter"; 
         cell.imageView.image = [UIImage imageNamed:@"twitter-bird-blue-on-white.png"]; 
       } 
       break; 

      } 
      case SectionDetail: { 


       //add webView to your cell 
       if (webViewDidFinishLoad == TRUE && indexPath.section != SectionComments) { 
        CGFloat contentHeight = webView.scrollView.contentSize.height; 
        webView.frame = CGRectMake(23, 10, 275, contentHeight); 
       } else { 
        webView.frame = CGRectMake(23, 10, 275, 10); 
       } 
       cell.backgroundColor = [UIColor whiteColor]; 
       [cell addSubview:webView]; 

       break; 
      } 
      case SectionComments: { 

       NSString *writerText = [[[self.commentParser.commentsArray objectAtIndex:indexPath.row] name] stringByAppendingString:@" schrieb:\n"]; 
       writerText = [writerText stringByAppendingString:[[self.commentParser.commentsArray objectAtIndex:indexPath.row] description]]; 
       writerText = [writerText stringByAppendingFormat:@"\n"]; 
       cell.textLabel.text = writerText; 
       cell.imageView.image = nil; 
       cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 
       cell.textLabel.numberOfLines = 0; //multiline 


       if (cell.textLabel.text == nil) { 
        cell.textLabel.text = @"Keine Kommentare vorhanden"; 
        cell.textLabel.font = [UIFont fontWithName:@"Verdana-Italic" size:12]; 
       } 
       break; 
      } 

     } 
    return cell; 
    } 
} 

Почему UIWebView в 4 клетки снова, и как я могу изменить это?

Спасибо за каждый ответ!

+0

Где/как вы определяете переменные в ваших операциях 'case' (например,' SectionHeader')? Возможно, вы случайно ошиблись в своих определениях 'SectionComments' и' SectionDetail'? – GeneralMike

+0

Это typedef's 'typedef enum {SectionHeader, SectionDetail, SectionComments} Разделы; typedef enum {SectionHeaderTitle, SectionHeaderDate, SectionHeaderSharerFacebook, SectionHeaderSharerTwitter} HeaderRows; typedef enum {SectionDetailImage, SectionDetailSummary} DetailRows; ' – Sebastian

ответ

-1

Причина в том, что UITableView повторно использует свои ячейки для сохранения памяти. На эту тему много вопросов, я бы рекомендовал вам прочитать некоторые (1, 2, ...). Короче говоря, когда вы делаете:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

для ячейки в колонтитул секции, вы получаете ячейку, которая ранее была в средней части, который содержит UIWebView. Вам нужно удалить подкатегорию UIWebView, прежде чем использовать ее как ячейку в нижнем колонтитуле.

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