0

我需要在每个单元格上设置两个按钮,它们具有不同的操作但相同的框架,单击一个将隐藏另一个将可见。我怎样才能做到这一点。提前致谢。

4

1 回答 1

0
- (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:38:00.500 回答