在每个单元格上设置两个按钮,具有不同的动作但相同的帧

时间:2012-10-10 06:04:39

标签: iphone uitableview

我需要在每个单元格上设置两个按钮,使用不同的动作但相同的框架,单击一个将隐藏其他将可见。我怎样才能做到这一点。 提前谢谢。

1 个答案:

答案 0 :(得分: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"];
    }
}