Hide UIButton of UITableviewcell

陌路散爱 提交于 2019-12-11 20:34:09

问题


I have one Simple critical Problem in my code .

On my Tableview when I pressed on my Edit button (which is on navigationbar) It takes me to edit method of UITableview . I want to hide button and label which is on My Cell .

Code :

I am adding my Button like this ..

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *currentCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    currentCell = nil;
    if (currentCell==nil)
    {
        currentCell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    BtnEmail=[UIButton buttonWithType:UIButtonTypeCustom];
    //BtnEmail = (UIButton *)[currentCell viewWithTag: 3];
    BtnEmail.frame=CGRectMake(265, 17, 25, 25);
    BtnEmail.tag=indexPath.row;
    //[BtnEmail setTitle:@"E-mail" forState:UIControlStateNormal];
    [BtnEmail setBackgroundImage:[UIImage imageNamed:@"email.png"] forState:UIControlStateNormal];
    [BtnEmail addTarget:self action:@selector(BtnEmail:) forControlEvents:UIControlEventTouchUpInside];
    [currentCell.contentView addSubview:BtnEmail];
    [currentCell.contentView bringSubviewToFront:BtnEmail];



    return currentCell;
}

My Edit button is Declare like this

Edit Button :

self.navigationItem.rightBarButtonItem = self.editButtonItem;

And On edit click My this method will call .

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [listTableView setEditing:editing animated:animated];

    if(editing)
    {
        self.navigationItem.leftBarButtonItem.enabled = NO;
        //BtnEmail.frame=CGRectMake(355, 17, 25, 25);
        BtnEmail.hidden = TRUE;
    }
    else
    {
        self.navigationItem.leftBarButtonItem.enabled = YES;
        //BtnEmail.frame=CGRectMake(265, 17, 25, 25);
        BtnEmail.hidden = FALSE;
    }

    [super setEditing:editing animated:animated]; 
}

In this case my Last cell Button and Lable gonna hide not all . I need to hide all the UIButton of my Cell .

Like if I have 3 cell on table then it will hide only last button only ..

Thanks .


回答1:


You can do it simply making a conditional check in cellForRow and reload the table view when the editing changed.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 // Do your other stuff

// Add this conditional check
if (tableView.editing)
{
    BtnEmail.hidden = YES;
}
else
{
    BtnEmail.hidden = NO;

}
[currentCell.contentView addSubview:BtnEmail];
[currentCell.contentView bringSubviewToFront:BtnEmail];

return currentCell;
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{

[super setEditing:editing animated:animated];
[listTableView setEditing:editing animated:animated];

// Reload the table view
[listTableView reloadData];
// Do your other stuff
 }

But it easy to create that button by using a custom cell. Here you are adding the button again and again. Otherwise move the button creating code to the if(cell == nil) block




回答2:


That is because you are declaring

BtnEmail.hidden = FALSE;

which in this case is your last allocated button in cellForRowAtIndexPath method. Try something like this:

    if(editing)
        {
            self.navigationItem.leftBarButtonItem.enabled = NO;
            //BtnEmail.frame=CGRectMake(355, 17, 25, 25);
             For(int i=0; i< [tableArray count]; i++)
        {
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ;
            UIButton *btn=(UIButton *)[cell viewWithTag:i];
            btn.hidden = TRUE;
        }
        }
  else
    {
        self.navigationItem.leftBarButtonItem.enabled = YES;
            //BtnEmail.frame=CGRectMake(265, 17, 25, 25);
       For(int i=0; i< [tableArray count]; i++)
      {
            UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] ;
            UIButton *btn=(UIButton *)[cell viewWithTag:i];
            btn.hidden = FALSE;
        }
        }


来源:https://stackoverflow.com/questions/15087434/hide-uibutton-of-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!