【问题标题】:UITableViewCell Added UISwitch on change show UIbutton on cellUITableViewCell 添加了 UISwitch on change 在单元格上显示 UIbutton
【发布时间】:2017-03-09 06:40:21
【问题描述】:

我想根据UISwitch 的开/关状态显示/隐藏UIButton。滚动 tableview UIButton 时会更改状态。它也显示在其他单元格上。请指教。 在UITableViewCell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    EnhancedChecklistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if ( cell == nil )
    {
        NSArray *topLevelObjects;
        if(IS_IPAD)
        {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell" owner:self options:nil];

        }
        else
        {
            topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"EnhancedChecklistTableViewCell_Iphone" owner:self options:nil];

        }
        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[EnhancedChecklistTableViewCell class]])
            {
                cell = (EnhancedChecklistTableViewCell *)currentObject;
                cell.delegate = self;
                break;
            }
        }
    }

    R5OPERATORACTCHECKLISTS *inspectionTasks = [_data objectAtIndex:indexPath.section];
    return cell;
}

所有开关都是使用自定义单元格中的 xib 文件添加的。我使用了一种在值更改事件后触发的方法。并根据我正在显示按钮的开关状态。但是在滚动时它会打扰。

【问题讨论】:

  • 您能否分享一些您尝试过的代码,以便我们提供帮助。
  • 细胞被重复使用;您不能依赖单元格本身的开关状态;您需要将开关状态存储在一些数据结构中,例如数组,并在cellForRowAt: 中使用它来重置单元状态
  • 请出示您的代码。 @斯瓦蒂
  • 我使用了自定义单元格。并添加了来自 xib 文件的开关。开关的值更改事件,例如 if (sender == _buttonFollowUp ) { self.followUp = !_followUp; self.btnSave.hidden = 否; } else if (sender == _buttonFinalOccurence ) { self.finalOccurence = !_finalOccurence; self.btnSave.hidden = 否; } 像这样添加 .. 但滚动 tableview 后 .. 保存按钮将出现在所有单元格上。
  • 你能告诉我你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { func 吗?我会帮你。 :)

标签: ios objective-c iphone ipad


【解决方案1】:

创建.h文件bool变量名“switchFlag”并默认设置YES然后你可以像这样签入cellForRowAtIndexPath方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
    }
    if (switchFlag ==YES)
    {
        NSLog(@"Switch On");
    }
    else
    {
        NSLog(@"Switch Off");
    }
}

当您单击UISwitch 时,该时间更改值 switchFlag 设置YESNO 取决于 UISwitch 状态,因此它会给您预期的输出,我希望它会对您有所帮助。

【讨论】:

    【解决方案2】:

    应该是这样的

    首先你可以使用 NSMutableArray ,开关的默认状态是 On/Off (1/0)。

    self.switchStateArray = [[NSMutableArray alloc]initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1", nil];
    

    并且在 cellForRowAtIndexPath 中,您可以在开关操作中调用以下方法

    cell.normalSwitch.tag = indexPath.row;
    
    [cell.normalSwitch addTarget:self action:@selector(switchToggle:) forControlEvents:UIControlEventValueChanged];
    
     cell.normalSwitch.userInteractionEnabled = YES;
    
    if (cell.normalSwitch.isOn)
    {
        cell.buttonName.hidden = NO;
    
        NSLog(@"Switch is On");
    }
    else
    {
        cell.buttonName.hidden = YES;
    
       NSLog(@"Switch is Off");
    
    }
    

    //切换动作

    -(void)switchToggle:(id)sender
    {
        UISwitch *theSwitch = (UISwitch *)sender;
    
        NSLog(@"tag %ld",(long)theSwitch.tag);
        NSInteger index = [sender tag];
    
        if (theSwitch.isOn)
        {
            [self.switchStateArray replaceObjectAtIndex:index withObject:@"1"];
            [theSwitch setOn:YES animated:YES];
        }
        else
        {
            [self.switchStateArray replaceObjectAtIndex:index withObject:@"0"];
            [theSwitch setOn:NO animated:YES];
        }
    
        [self.tableviewName reloadData];
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多