0

我有一个 UITable 可以在标题部分单击时折叠。我需要标题部分在其中包含唯一的文本。

我可以使用 cell.textlabel.text = @""; 这很好用,但是我想在单元格中添加更多标签并定位它们,即

            lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
            lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
            lbl123.textColor = [UIColor blackColor];
            lbl123.backgroundColor = [UIColor clearColor];
            lbl123.highlightedTextColor = [UIColor whiteColor];
            lbl123.opaque = NO;
            lbl123.adjustsFontSizeToFitWidth = TRUE;
            lbl123.numberOfLines = 0;
            lbl123.lineBreakMode = UILineBreakModeWordWrap;
            [cell addSubview:lbl123];
            lbl123.text = @"test";

但是当我将它添加到“cellforindexpath”部分时,它会在表格下方的单元格上复制自身。

任何想法为什么会这样?

下面的完整代码: -

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellValue = 0;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:@"MyIdentifier"];
    }

    if (tableView.tag == 2)
    {
        UILabel *lbl123;
        if ([self tableView:tableView canCollapseSection:indexPath.section])
        {
            cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];

            if (!indexPath.row)
            {
                cell.textLabel.backgroundColor = [UIColor clearColor];
                UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 60)];
                [bgView setBackgroundColor:[UIColor blackColor]];
                [bgView setAlpha:0.3];
                [cell setBackgroundView:bgView];
                lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
                lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
                lbl123.textColor = [UIColor blackColor];
                lbl123.backgroundColor = [UIColor clearColor];
                lbl123.highlightedTextColor = [UIColor whiteColor];
                lbl123.opaque = NO;
                lbl123.adjustsFontSizeToFitWidth = TRUE;
                lbl123.numberOfLines = 0;
                lbl123.lineBreakMode = UILineBreakModeWordWrap;
                [cell addSubview:lbl123];
                lbl123.text = @"";

                if(indexPath.section == 1)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"My Results Overview:"];
                    cell.textLabel.textColor = [UIColor whiteColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;

                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 80)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    av.image = [UIImage imageNamed:@"Blue bar 720x115.png"];
                    cell.backgroundView = av;
                }

                if(indexPath.section == 2)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"Result1:"];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;


                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    if ([confidenceCOL isEqualToString:@"Red"])
                    {
                        av.image = [UIImage imageNamed:@"redPanel.png"];
                    }
                    if ([confidenceCOL isEqualToString:@"Green"])
                    {
                        av.image = [UIImage imageNamed:@"greenPanel.png"];
                    }
                    if ([confidenceCOL isEqualToString:@"Purple"])
                    {
                        av.image = [UIImage imageNamed:@"purplePanel.png"];
                    }
                    cell.backgroundView = av;


                    lbl123.text = @"Test";
                }
                if(indexPath.section == 3)
                {
                    cell.textLabel.font = [UIFont fontWithName:@"Trebuchet MS-Bold" size:15.0f];
                    cell.textLabel.text = [NSString stringWithFormat:@"Result2:"];
                    cell.textLabel.textColor = [UIColor blackColor];
                    cell.textLabel.numberOfLines = 3;
                    cell.textLabel.opaque = YES;

                    UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 294, 120)];
                    av.backgroundColor = [UIColor clearColor];
                    av.opaque = NO;
                    if ([ambitionCOL isEqualToString:@"Red"])
                    {
                        av.image = [UIImage imageNamed:@"redPanel.png"];
                    }
                    if ([ambitionCOL isEqualToString:@"Green"])
                    {
                        av.image = [UIImage imageNamed:@"greenPanel.png"];
                    }
                    if ([ambitionCOL isEqualToString:@"Purple"])
                    {
                        av.image = [UIImage imageNamed:@"purplePanel.png"];
                    }
                    cell.backgroundView = av;

                    lbl123.text = @"TestING!";
                }

                if ([expandedSections containsIndex:indexPath.section]){}
                else{}

            }
            else
            {
                if(indexPath.section == 2)
                {
                    cellValue = Result1Moretext;
                }
                if(indexPath.section == 3)
                {
                    cellValue = Result2Moretext;
                }


                static NSUInteger const k1 = 1;

                // Declare references to the subviews which will display the data.
                UILabel *lbl1 = nil;

                static NSString *kCellID = @"CellID";

                UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
                if (cell == nil) {
                    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellID];

                    lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 300)];
                    lbl1.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
                    lbl1.tag = k1;
                    lbl1.textColor = [UIColor blackColor];
                    lbl1.backgroundColor = [UIColor clearColor];
                    lbl1.highlightedTextColor = [UIColor whiteColor];
                    lbl1.opaque = NO;
                    lbl1.adjustsFontSizeToFitWidth = TRUE;
                    lbl1.numberOfLines = 0;

                    lbl1.lineBreakMode = UILineBreakModeWordWrap;
                    [cell.contentView addSubview:lbl1];


                }else {
                    lbl1 = (UILabel *)[cell.contentView viewWithTag:k1];
                }

                cell.accessoryView.tag = indexPath.row;

                lbl1.text = [NSString stringWithFormat:@"%@", cellValue];
                return cell;

            }

        }



        else
        {

        }


    }

谢谢

4

1 回答 1

1

不要忘记在 UITableViews 中重用单元格以保持较低的内存使用率。所以发生的事情是你正在制作一个单元格,并向其中添加标签,然后在该单元格滚动屏幕后,它进入一个重用池,被重新添加为更下方的新单元格,并且其中仍然有标签

您应该做的是在创建新单元格时创建所有标签等(在 cell==nil 部分,这是一个单元格未出列的地方),并在其余代码中设置所有标签。

您可以稍后使用视图标签再次查找标签视图。

例如。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:@"MyIdentifier"];
    lbl123 = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 290, 20)];
    lbl123.font = [UIFont fontWithName:@"Trebuchet MS" size:12.0f];
    lbl123.textColor = [UIColor blackColor];
    lbl123.backgroundColor = [UIColor clearColor];
    lbl123.highlightedTextColor = [UIColor whiteColor];
    lbl123.opaque = NO;
    lbl123.adjustsFontSizeToFitWidth = TRUE;
    lbl123.numberOfLines = 0;
    lbl123.lineBreakMode = UILineBreakModeWordWrap;
    [cell addSubview:lbl123];
    lbl123.text = @"";
    lbl123.tag=123;
} else {
    lbl123 = (UILabel *)[cell viewWithTag:123];
}

if(indexPath.section == 2)
{
    lbl123.text = @"Test";
}
于 2012-11-17T22:30:34.440 回答