如何在iPhone中通过tableview的indexpath更改切换按钮?

时间:2012-02-03 11:32:18

标签: iphone objective-c

我有一个tableview,我在tableview单元格中显示数据库中的值。在这个tableview cellforrowatindexpath中,我创建了一个带有图像的按钮,并为其设置了选择器。因此,当我点击按钮时,我的选择器被调用,它将我的图像更改为另一个图像。

但问题是它没有识别索引路径,即如果在tableview中存在4行,如果我点击第一行按钮,它的图像应该改变但问题是第4行动作被执行并且其图像被更改,因为它没有得到要更改的图像的正确索引路径。

这是我的cellforrowatindexpath代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
    cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

        mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        mimageButton.frame=CGRectMake(10, 20, 20, 20);   
        mimageButton.tag = 1;
        onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
        onButtonView.tag = 2;
        onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
        [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
        [cell.contentView addSubview:mimageButton];

        [mimageButton addTarget:self action:@selector(changeMapType::) forControlEvents:UIControlEventTouchUpInside];


}




     return cell;

}

这是我的changemapType代码

-(void)changeMapType:(NSIndexPath*)path1:(UIButton*)sender{
appDelegate.changeimagetype =!appDelegate.changeimagetype;
if(appDelegate.changeimagetype == YES)
{
    onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
    [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
    appDelegate.changeimagetype = YES;
}
else
{
    onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
    [mimageButton setImage:onButtonView.image forState:UIControlStateNormal];
    appDelegate.changeimagetype = NO;

}


}

5 个答案:

答案 0 :(得分:1)

不要破坏表视图单元重用只是为了在每个按钮上放置不同的标记。如果您的单元格相同,请使用相同的重用标识符。

您可以像这样找到发件人的索引路径,而无需乱搞标签。它也适用于多节表。

CGPoint hitPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *hitIndex = [self.tableView indexPathForRowAtPoint:hitPoint];

答案 1 :(得分:0)

我认为你不能在@selector中有其他参数。您可能要做的是继承UIButton并添加NSIndexPath属性(甚至只是int)并在-(void)changeMapType:(MyCustomButton*)sender中访问该属性。

< S>

您似乎甚至不使用changeMapType中的索引路径,因此也需要更改。

编辑:是您要更改的按钮图片吗?在这种情况下,根本不需要索引路径或子类。只需使用[sender setImage:(UIImage *)image forState:(UIControlState)state]

答案 2 :(得分:0)

使用yourTableView获取索引路径..尝试这样的事情。

  - (void)buttonAction:(UIButton*)sender {

        UITableViewCell *cell = (UITableViewCell*)button.superview;
        NSIndexPath *indexPath = [yourTableView indexPathForCell:cell];
        NSLog(@"%d",indexPath.row)
    }

答案 3 :(得分:0)

我认为以下代码可能对您有帮助..


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

    UIView *myCheckbox = [[UIView alloc] init];
    UIButton *myBt1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];

    if([appDelegate.changeimagetype == YES"]){
       [myBt1 setBackgroundImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];       
    }
    else {
       [myBt1 setBackgroundImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];       
    }
    [myBt1 addTarget:self action:@selector(btcheckbox:) forControlEvents:UIControlEventTouchDown];
    [myBt1 setTag:indexPath.row];
    [myCheckbox addSubview:myBt1];

    [myBt1 release];
    [myCheckbox release];
    [cell addsubview:myview];
    return cell;    
}

-(void) btcheckbox:(id) sender
{
    UIButton *currentButton = (UIButton *) sender;

    if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"alarm_OF.png"]])
    {
         appDelegate.changeimagetype = YES;
         [currentButton setBackgroundImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];

    }else if([[currentButton backgroundImageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"alarm_ON.png"]])
    {
         appDelegate.changeimagetype = NO;
         [currentButton setBackgroundImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];
    }
}

答案 4 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                   NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d", indexPath.row];
                appDelegate = (StopSnoozeAppDelegate*)[[UIApplication sharedApplication]delegate];
                cell =(TAlarmCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
                if (cell == nil) {
                    cell = [[[TAlarmCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

                   UIButton *mimageButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    mimageButton.frame=CGRectMake(10, 20, 20, 20);   
                    mimageButton.tag = indexPath.row;
                    //onButtonView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 50)];
                    //onButtonView.tag = 2;
                   // onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
                    [mimageButton setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
                    mimageButton.selected = NO;
                    [cell.contentView addSubview:mimageButton];

                    [mimageButton addTarget:self action:@selector(changeMapType:) forControlEvents:UIControlEventTouchUpInside];
          }
    return cell;
    }




-(void)changeMapType:(UIButton*)sender{

    if(sender.selected == YES)
    {
            // onButtonView.image = [UIImage imageNamed:@"alarm_OF.png"];
        [sender setImage:[UIImage imageNamed:@"alarm_ON.png"] forState:UIControlStateNormal];
        sender.selected = NO;
    }
    else
    {
            //onButtonView.image = [UIImage imageNamed:@"alarm_ON.png"];
        [sender setImage:[UIImage imageNamed:@"alarm_OF.png"] forState:UIControlStateNormal];
        sender.selected = YES;

    }

}