隐藏tableview中特定单元格上的按钮

时间:2012-10-09 13:51:03

标签: iphone uitableview

我在每个单元格上使用两个按钮,相同的框架,一个将永远隐藏,我需要按钮单击该单元格的按钮被隐藏或隐藏不是全部。

2 个答案:

答案 0 :(得分:1)

实现它的一种方法是有效地使用标签..

分配按钮时将标签设置为..

// in cellForRowAtIndexPath

[forwardButton setTag:indexPath.row + 1];
[backButton setTag:(indexPath.row + 1) * 1000]; //possible to avoid clash of tags

为这两个按钮设置相同的操作方法

现在在目标方法中......假设 - (无效)切换;

   -(void)toggle:(UIButton *)sender{
     NSIndexPath *index = [NSIndexPath indexPathForRow:sender.tag inSection:0];

        UITableViewCell *cell = [t cellForRowAtIndexPath:index];

    if(sender.tag < 999){  //differentiate which button is clicked

        UIButton *forwardButtonToBeHidden = [cell.contentView viewWithTag:tag];

        UIButton *backButtonToBeEnabled = [cell.contentView viewWithTag:tag * 1000];

        [cell sendSubviewToBack:forwardButtonToBeHidden];

    [forwardButtonToBeHidden setHidden:YES];
    [forwardButtonToBeHidden setEnabled:NO];
        [cell bringSubviewToFront:backButtonToBeEnabled];
    [backButtonToBeEnabled setHidden:NO];
    [backButtonToBeEnabled setEnabled:YES];

    }
    else{
     UIButton *forwardButtonToBeHidden = [cell.contentView viewWithTag:tag ];

        UIButton *backButtonToBeEnabled = [cell.contentView viewWithTag:tag/1000];

        [cell sendSubviewToBack:forwardButtonToBeHidden];

    [forwardButtonToBeHidden setHidden:YES];
    [forwardButtonToBeHidden setEnabled:NO];
        [cell bringSubviewToFront:backButtonToBeEnabled];
    [backButtonToBeEnabled setHidden:NO];
    [backButtonToBeEnabled setEnabled:YES];

    }
}

答案 1 :(得分:-1)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell * cell = 
[tableHistory dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle    reuseIdentifier:SimpleTableIdentifier] autorelease];
}

else
{

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];

if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"check"])

    [button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
else
    [button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];

[button addTarget:self   action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:button];


cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];

cell.detailTextLabel.text = [arrayTktcode objectAtIndex:indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

[cell addSubview:btnUnScan];

[cell addSubview:btnUScan];

return cell;

}

-(void)buttonClicked:(id)sender
{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableHistory];
NSIndexPath *indexPath = [tableHistory indexPathForRowAtPoint:touchPoint];

UIButton *button = (UIButton *)sender;

if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
    [button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
    [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"check"];
}
else
{
    [button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];
    [arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}