【问题标题】:Get value from a UISlider inside a Cell从单元格内的 UISlider 获取值
【发布时间】:2011-12-01 21:36:15
【问题描述】:

我有一个tableView,其中包含自定义单元格,每个单元格都有一个UISlider

现在我想创建一个方法来获取那些UISliders 的值,但我不知道如何访问每个单元格及其滑块值。

提前致谢!

【问题讨论】:

    标签: iphone ios uitableview uislider


    【解决方案1】:

    首先为每个滑块添加一个标签,以便您不知道哪个滑块是哪个:

    slider.tag = 0 //Can be any unique integer

    然后注册一个改变滑块的方法:

    [slider addTarget:self action:@selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];

    终于在你的方法中

    -(void)sliderUpdate:(UISlider *)sender {
        int value = sender.value;
        int tag = sender.tag;
    }
    

    标签现在将是您之前使用的唯一整数。这是一种识别元素的方式。

    【讨论】:

    • 我应该在滑块更新内的标签之间切换吗?还是只是“如果”通过我的 13 个标签? :P
    • 我实际上会使用循环:for (int i = 0; i < numberOfCells; i++){ if (i == slider.tag) {//this is the cell that corresponds to this slider}}
    【解决方案2】:

    嗯,可能性很小:

    1) 保留对 UITableViewController 中所有单元格的引用

    在您的tableView:cellForRowAtIndexPath: 中,在返回单元格之前,将其添加到您作为 UITableViewController 上的属性的数组中

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
          //Set up your cell
    
          [[self allCells] addObject:cell];
          return cell;
    }
    
    -(void)getSliderValues {
        for(CustomCell *cell in [self allCells]){
            //Here is your cell
            cell;
        }
    }
    

    2) 使用tableView:cellForRowAtIndexPath:

    -(void)getSliderValues {
        int section = 0;
        int numberOfCells = [self tableView:[self tableView] numberOfRowsInSection:section];
    
        for(int row = 0; row < numberOfCells; row++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    
            //Here is your cell
            CustomCell *cell = [self tableView:[self tableView] cellForRowAtIndexPath:indexPath];
        }
    }
    

    请注意,在此方法中,如果您使用重用标识符,您可能不会得到您想要/需要的单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      相关资源
      最近更新 更多