【问题标题】:Hiding the table view cells with switch ON/OFF in iPhone在 iPhone 中使用开关 ON/OFF 隐藏表格视图单元格
【发布时间】:2012-02-18 06:26:56
【问题描述】:

对于各位专家来说,这可能是一个简单的问题,但我无法找出正确的解决方案。

在我的应用程序中,我创建了一个表格视图来列出项目数量。

在单元格的第一行有一个开关 (ON/OFF)。

所以我的客户需要在 switch 为 OFF 时隐藏底部单元格(第 0 个单元格除外),并在 switch 为 ON 时显示所有单元格。

我通过代码创建了开关。

谁能帮我解决这个问题?

提前致谢。

【问题讨论】:

    标签: iphone objective-c xcode


    【解决方案1】:

    numberOfRows dataSource 方法 - 使其返回 1 并重新加载TableView

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
      /// ....
                self.switchView = [[[UISwitch alloc] initWithFrame:CGRectZero]autorelease];
                cell.accessoryView = self.switchView;
                [self.switchView setOn:NO animated:NO];
                [self.switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
    
    //....
    
    }
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
       if [self.switchView.on]
           return 1;
       else 
       {
          // normal code return
       }
    }
    
    - (void) switchChanged:(id)sender {
         [self.tableView reloadData];
    } 
    

    【讨论】:

    • @mbh,它是 UITableVIewDataSource 方法,而不是委托方法。
    • 我们不能像iOS原生设置应用一样隐藏和显示下面的单元格动画吗?
    【解决方案2】:

    你可以使用insertRowsAtIndexPaths,这不仅会添加,还会做添加新行的动画

    首先将目标UIControlEventTouchUpInside 添加到UISwitch

    [switch addTarget:self action:@selector(switchChange:) forControlEvents:UIControlEventValueChanged];

    -(void) switchChange:(UISwitch*) 发送者{

           isSwitchOn = sender.on; //edited
        [myTable beginUpdates];
        NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
        NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
        //you may add as many row as you want here.
    
        [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil] withRowAnimation:UITableViewRowAnimationMiddle];
        [myTable endUpdates];
    

    }

    并确保在numberOfRowsInSection: 中添加要插入的行数

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

      if (isSwitchOn) 返回 1;

      否则 返回 3; //开启后会有多少行

    }

    您可能需要检查[myTable insertSections:withRowAnimation: 以插入另一个部分并执行与上述相同的操作。但请记住在numberOfSectionsInTableView: 中说明您将添加多少部分

    更新:

    如果关闭则执行deleteRowsAtIndexPaths:

    -(void) switchChange:(UISwitch*) 发送者{

        isSwitchOn = sender.on;
    
      if(isSwitchOn){
            [myTable beginUpdates];
             NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
             NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
             //you may add as many row as you want here.
    
            [myTable insertRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil]  withRowAnimation:UITableViewRowAnimationMiddle];
            [myTable endUpdates];
        }
      else{
            [myTable beginUpdates];
             NSIndexPath *row1 = [NSIndexPath indexPathForRow:1 inSection:0];
             NSIndexPath *row2 = [NSIndexPath indexPathForRow:2 inSection:0];
             //you may add as many row as you want here.
    
            [myTable deleteRowsAtIndexPaths:[NSArray arrayWithObjects:row1,row2,nil]  withRowAnimation:UITableViewRowAnimationFade];
            [myTable endUpdates]; 
     }
    

    }

    【讨论】:

      【解决方案3】:

      有一个 UITableViewDataSource 方法称为 tableView:numberOfRows:inSections 方法。只需检查开关的状态,如果它是 OFF 则返回 1,如果它是 ON,则返回所有行。每次打开和关闭时只需调用 [tableView reloadData] 方法。就这么简单。

      【讨论】:

      • 当 reloadData 方法被调用时,它会依次调用以下方法: 1. tableView:numberOfSections: 2 tableView:numberOfRows:inSection: 3. tableView:cellForRowAtIndexPath: methodfs。所以你必须在打开和关闭时调用它。
      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 1970-01-01
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 2015-08-10
      • 1970-01-01
      相关资源
      最近更新 更多