折叠UITable部分

时间:2012-11-17 21:53:51

标签: xcode uitableview

我有一个可以在标题部分点击时折叠的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
        {

        }


    }

由于

1 个答案:

答案 0 :(得分:1)

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

当您创建一个新单元格时(在单元格== 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";
}