如何从额外添加的行UITableViewCell中删除/隐藏内容视图(UIButton)

时间:2012-02-14 09:11:30

标签: iphone uitableview

当我没有添加任何额外的行时,下面的代码工作正常,所有内容视图都被控制(隐藏/可见)但添加到虚拟行时出现问题。
我正在以编程方式创建一个表视图。我正在使用一些内容视图(标签和按钮)初始化我的单元格。我正在添加两个虚拟行:if indexPath.row> [mYarray计数]。我试图在额外添加的行中隐藏我的按钮。当我滚动按钮有时隐藏在单元格或某些时间显示在单元格中。条件类似于:if(indexPath.row&lt ; [mYarray计数])然后按钮MAY(见下面的代码,决定隐藏/可见的情况if(ShowNQButton))是隐藏/可见。但是if(indexPath.row> [mYarray count ])然后按钮必须隐藏/隐藏/删除。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];



    if (cell == nil) 
    {

        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:Identifier] autorelease];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;      

        waitTimeLabel=[[[UILabel alloc] initWithFrame:CGRectMake(210, 0, 50, 30)]autorelease];
        waitTimeLabel.backgroundColor=[UIColor clearColor];
        waitTimeLabel.tag=lblTAG5;
        waitTimeLabel.numberOfLines=2;
        waitTimeLabel.font=[UIFont systemFontOfSize:13];
        [cell.contentView addSubview:waitTimeLabel];

        EstSeatLabel=[[[UILabel alloc] initWithFrame:CGRectMake(270, 0, 60, 30)]autorelease];
        EstSeatLabel.backgroundColor=[UIColor clearColor];
        EstSeatLabel.tag=lblTAG6;
        EstSeatLabel.font=[UIFont systemFontOfSize:12];
        [cell.contentView addSubview:EstSeatLabel];

    }
    else
    {   

        waitTimeLabel=(UILabel *)[cell viewWithTag:lblTAG5]; 
        EstSeatLabel=(UILabel *)[cell viewWithTag:lblTAG6];         

    } 
if (indexPath.row < [UIAppDelegate.waitlistDetailArray count]) {


        NQbutton =   [[UIButton alloc]init];
        NQbutton.frame=CGRectMake(220, 4, 42, 24);
        NQbutton.adjustsImageWhenHighlighted=NO;
        NQbutton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        NQbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        UIImage *NQbuttonimage = [UIImage imageNamed:@"NqN"];
        [NQbutton setBackgroundImage:NQbuttonimage forState:UIControlStateNormal];
        [NQbutton addTarget:self action:@selector(NQbutton:event:) forControlEvents:UIControlEventTouchUpInside];

        NQbutton.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:NQbutton];
        [NQbutton release];

        NQbuttonEstTime=   [[UIButton alloc]init];
        NQbuttonEstTime.frame=CGRectMake(275, 4, 42, 24);
        NQbuttonEstTime.adjustsImageWhenHighlighted=NO;
        NQbuttonEstTime.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        NQbuttonEstTime.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

        UIImage *NQbuttonesttimeimage = [UIImage imageNamed:@"NqN"];
        [NQbuttonEstTime setBackgroundImage:NQbuttonesttimeimage forState:UIControlStateNormal];
        [NQbuttonEstTime addTarget:self action:@selector(NQbutton:event:) forControlEvents:UIControlEventTouchUpInside];

        NQbuttonEstTime.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:NQbuttonEstTime];      
        [NQbuttonEstTime release];

        if(ShowNQButton)//**this BOOL variable is outside of cellForRow decides if buttons  //should be visible or not when indexPath.row < myArray' count.**
        {
            NQbutton.hidden=NO;
            NQbuttonEstTime.hidden=NO;

        }
        else {
            NQbutton.hidden=YES;
            NQbuttonEstTime.hidden=YES;

        }


    } 
else
    {//i have added three dummy rows in numberOfRowsInSection(when indexpath.row > [myArray Count])
        waitTimeLabel.text=@" "; 
    EstSeatLabel.text=@" ";

        //below i am stuffing in removing/hiding these two contentViews from dummy cells 
        NQbutton.hidden=YES;//tried setImage=nil fornormalState//tried removeFromSupViw
        NQbuttonEstTime.hidden=YES;//tried setImage=nil fornormalState//tried removeFromSupViw
    }


}


return cell;

}

有什么建议吗? 感谢

1 个答案:

答案 0 :(得分:0)

问题在于出列单元格。如果查看代码,则只在indexPath.row<[array cont]上添加两个按钮,逻辑正确。 请尝试使用提供的解决方案here