【问题标题】:Hide UIButton of UITableviewcell隐藏 UITableviewcell 的 UIButton
【发布时间】:2013-02-26 11:03:14
【问题描述】:

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

当我按下我的编辑按钮(在导航栏上)时,在我的 Tableview 上它需要我编辑 UITableview 的方法。我想隐藏 My Cell 上的按钮和标签。

代码:

我正在像这样添加我的按钮..

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

我的编辑按钮是这样声明的

编辑按钮:

self.navigationItem.rightBarButtonItem = self.editButtonItem;

并且在编辑时单击我的这个方法将调用 .

- (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 。

如果 我在桌子上有 3 个单元格,那么它只会隐藏最后一个按钮 ..

谢谢。

【问题讨论】:

  • 这将是因为在这种情况下,您将获得最后创建的 BtnEmail 引用。

标签: iphone ios uitableview show-hide


【解决方案1】:

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

- (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)

【讨论】:

    【解决方案2】:

    那是因为你在声明

    BtnEmail.hidden = FALSE;
    

    在这种情况下,这是您在 cellForRowAtIndexPath 方法中最后分配的按钮。试试这样的:

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

    【讨论】:

    • 上层工作正常。感谢您的帮助。 @iPhone 开发者。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多