如何在UITableViewCell中处于编辑模式时隐藏按钮

时间:2012-08-11 08:18:27

标签: iphone ios5 xcode4.3

编辑模式时,我可以在单元格中隐藏按钮吗?我的单元格由数组填充,保存在plist中。这是我的代码

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [titleArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"the indexpath.row = %i",indexPath.row);

    UITableViewCell *cell = [[UITableViewCell alloc]init];

    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:CGRectMake(250.0, 25.0, 30.0, 30.0)];
    [button setTitle:@"!" forState:UIControlStateNormal];
    [button setTitle:@"X" forState:UIControlStateSelected];
    [button setTag:indexPath.row + 100];
    [button addTarget:self action:@selector(onoffBtn:) forControlEvents:UIControlEventTouchUpInside];

    // my label code

    UIImageView *bgCell = [[UIImageView alloc]initWithFrame:CGRectMake(0,0 , 320, 80)];
    bgCell.image = [UIImage imageNamed:@"BG.jpeg"];

    [cell.contentView addSubview:button];
    // other cell contentView addSubview everything

    return cell;
    [tableView reloadData];
}

这是我的编辑按钮

- (IBAction)editBtn:(id)sender
{
    UIButton *editBtn = (UIButton *)sender;
    editBtn.selected = !editBtn.selected;

    if (editBtn.selected)
    {
        button.hidden = YES;
        [self.tableView setEditing:YES animated:YES];
    }
    else
    {
        button.hidden = NO;
        [self.tableView setEditing:NO animated:YES];
    }
}

上面的代码,我的按钮只隐藏在最后一个单元格中,只是说如果我有3个单元格。在编辑模式下,如何隐藏所有按钮隐藏?我希望所有按钮都隐藏。下面是我的onoffBtn。

- (IBAction)onoffBtn:(id)sender
{
    tempIndexPath = [_tableView  indexPathForCell:(UITableViewCell*)[[sender superview] superview]];
    UITableViewCell *cell = [_tableView cellForRowAtIndexPath:tempIndexPath];
    UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100];

    onoffBtn.selected = !onoffBtn.selected;
    if (onoffBtn.selected)
    {
        // start
    }
    else
    {
        // stop
    }
}

请教我更多。谢谢。

1 个答案:

答案 0 :(得分:1)

您必须为表的所有UiTableViewCell调用此方法:

 if (editBtn.selected)
        {

for (int row = 0, rowCount = [_iTableView numberOfRowsInSection:0]; row < rowCount; ++row) {
        UITableViewCell *cell = [_iTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];

      UIButton *onoffBtn = (UIButton *)[cell.contentView viewWithTag:tempIndexPath.row+100];
     onoffBtn.hidden=YES;
    }
   }