【问题标题】:How to pass change the toggle button through indexpath of tableview in iPhone?如何通过 iPhone 中 tableview 的 indexpath 传递更改切换按钮?
【发布时间】:2012-02-03 11:32:18
【问题描述】:

我有一个表格视图,我在表格视图单元格中显示来自数据库的值。在这个 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;

}


}

【问题讨论】:

  • mimageButton.tag 根据您的 cellidentfier 设置此标签值。并且不需要在选择器中发送任何东西作为参数
  • 你做错事了
  • @Ron 能否请您详细说明一下
  • @Rani 你有多个部分吗?
  • @Narayana 我没有多个部分

标签: iphone objective-c


【解决方案1】:

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

你可以像这样找出发送者的索引路径,而不需要乱搞标签。它也适用于多节表。

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

【讨论】:

    【解决方案2】:

    我认为你不能像这样在@selector 中添加额外的参数。您可能需要做的是继承 UIButton 并添加一个 NSIndexPath 属性(或者甚至只是一个 int)并在 -(void)changeMapType:(MyCustomButton*)sender 中访问该属性。

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

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

    【讨论】:

      【解决方案3】:

      使用yourTableView 获取索引路径。试试这样的方法。

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

      【讨论】:

        【解决方案4】:

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


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

        【讨论】:

        • 如果您有任何问题,请询问我,关于上述答案。
        【解决方案5】:
        - (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;
        
            }
        
        }
        

        【讨论】:

        • 此代码不起作用。问题是当它们在 tableview 中是 3 行时,当我单击第一行 tableview 按钮时,最后一行的图像正在改变。可能是什么问题。谢谢
        猜你喜欢
        • 2019-04-23
        • 1970-01-01
        • 1970-01-01
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多