隐藏UITableviewcell的UIButton [英] Hide UIButton of UITableviewcell

查看:64
本文介绍了隐藏UITableviewcell的UIButton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有一个简单的关键问题.

I have one Simple critical Problem in my code .

在我的Tableview上,当我按下编辑"按钮(位于导航栏上)时,需要我编辑UITableview的方法.我想隐藏我的手机"上的按钮和标签.

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 .

代码:

我正在像这样添加按钮.

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

编辑"按钮:

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]; 
}

在这种情况下,我的Last cell Button和Lable不会全部隐藏.我需要隐藏Cell的所有UIButton.

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

就像我在桌子上有3个单元格一样,它只会仅隐藏最后一个按钮 ..

谢谢.

推荐答案

您可以简单地在 cellForRow 中进行条件检查,并在更改编辑后重新加载表格视图.

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
 }

但是使用自定义单元格可以轻松创建该按钮.在这里,您一次又一次地添加按钮.否则,将创建按钮的代码移到 if(cell == nil)

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

这篇关于隐藏UITableviewcell的UIButton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆