0

我有一个带有静态单元格的 TableView,其标题有 4 个单元格,中间的一个单元格带有 UIWebView,页脚带有注释部分。每个评论都有自己的单元格。我的问题是如果页脚有 4 条或更多评论,页脚中的第 4 个单元格带有与中间单元格相同的 UIWebView。

我的 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 个单元格中,我该如何更改?

感谢您的每一个回答!

4

1 回答 1

-1

原因是UITableView重用它的细胞来节省内存。关于这个主题有很多问题,我建议您阅读一些(1、2、...)简而言之,当你这样做时:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

对于页脚部分中的单元格,您会得到一个以前位于中间部分的单元格,其中包含UIWebView. 您需要先删除UIWebView子视图,然后再将其用作页脚部分中的单元格。

于 2013-05-21T20:33:35.493 回答