0

我在每个具有相同框架的单元格上使用两个按钮,一个将始终隐藏,我需要那个曾经单击的按钮,仅该单元格的按钮被隐藏或不隐藏。

4

2 回答 2

1

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

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

// in cellForRowAtIndexPath

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

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

现在在目标方法中..假设 -(void)toggle;

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

    }
}
于 2012-10-09T14:16:18.280 回答
-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"];
}
}
于 2012-10-10T09:44:09.587 回答