【问题标题】:TableView mix up the cellsTableView 混合单元格
【发布时间】:2013-05-21 18:12:20
【问题描述】:

我有一个带有静态单元格的 TableView,其标题有 4 个单元格,中间的一个单元格带有 UIWebView,页脚带有 cmets 部分。每个评论都有自己的单元格。 我的问题是如果页脚有 4 个或更多 cmets,页脚中的第 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 个单元格中,我该如何更改?

感谢您的每一个回答!

【问题讨论】:

  • 您在哪里/如何定义 case 语句中的变量(如 SectionHeader)?您是否可能不小心混淆了SectionCommentsSectionDetail 的定义?
  • 他们是typedef的typedef enum { SectionHeader, SectionDetail, SectionComments } Sections; typedef enum { SectionHeaderTitle, SectionHeaderDate, SectionHeaderSharerFacebook, SectionHeaderSharerTwitter } HeaderRows; typedef enum { SectionDetailImage, SectionDetailSummary } DetailRows;

标签: iphone ios uitableview


【解决方案1】:

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

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    相关资源
    最近更新 更多