简体   繁体   English

滑块更改会影响其他表格单元格

[英]slider change affects other table cells

Each of my table cells has a slider and a label to show its value. 我的每个表格单元格都有一个滑块和一个标签来显示其值。 If I move the slider while keeping all touches inside the cell, the label gets updated as expected. 如果在单元格内保持所有接触的同时移动滑块,标签将按预期更新。 However, if while dragging a slider, the touches cross into another cell, the label for the second cell gets updated while the first cell slider's thumb is moved. 但是,如果在拖动滑块时触摸到另一个单元格,则在移动第一个单元格滑块的拇指时,第二个单元格的标签将更新。 How can I restrict the label change to the first cell from which the slider event originated? 如何将标签更改限制为滑块事件起源的第一个单元格?

-(IBAction)sliderChanged:(id)sender forEvent:(UIEvent*)event
{    
    NSSet *allTouches = [event allTouches];
    UITouch *firstTouch = [[allTouches allObjects] objectAtIndex:0];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:[firstTouch locationInView:self.tableView]];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    if (cell)
    {
        UISlider *slider = (UISlider*)sender;
        UILabel *sliderValueLabel = (UILabel*)[cell viewWithTag:kSliderValueLabelTag];
        sliderValueLabel.text = [NSString stringWithFormat:@"%0.2f", slider.value];
    }
}

Your code looks reasonable to me, although to be consistent, I would get the cell from the "slider" rather than relying on the coordinates of the touch points. 您的代码对我来说似乎很合理,尽管要保持一致,我还是从“滑块”中获取单元格,而不是依靠接触点的坐标。 It may be that the "allTouches" array doesn't go back as far as you'd like to the original press. 可能是“ allTouches”数组没有返回到您想要的原始印刷机。

To do this, you would need to get the superView of the slider, and then use that to get the viewWithTag:. 为此,您需要获取滑块的superView,然后使用它来获取viewWithTag:。

Assuming that the slider and label are contained within the same view, you could try something like: 假设滑块和标签包含在同一视图中,则可以尝试执行以下操作:

UIView *cellContentView = slider.superView;
UILabel *sliderValueLabel = [cellContentView viewWithTag:kSliderValueLabelTag];

If you don't want to rely on view hierarchies, you could also put a tag on the slider that corresponds to the row in your table, and then use that to pull the cell. 如果不想依赖视图层次结构,也可以在与表中的行相对应的滑块上放置一个标签,然后使用该标签拉单元格。

When you move your slider, it is continuously calling your sliderChanged method. 当您移动滑块时,它会不断调用slideChanged方法。 And in this method you change the label of the cell in which the touch is, that is the wrong cell when your touch goes in another cell. 在这种方法中,您可以更改触摸所在的单元格的标签,即当您触摸另一个单元格时,该单元格就是错误的单元格。

What I would do: 我会怎么做:

  1. declare the sliderChange method in a customed class that extends UITableViewCell 在扩展UITableViewCell的自定义类中声明slideChange方法
  2. link the slider action to this method, so as it is in the cell, you always have the right cell 将滑块动作链接到此方法,因此它就像在单元格中一样,始终具有正确的单元格
  3. change the label of this cell that has the method with self.label 更改具有self.label方法的此单元格的标签

You can also: 你也可以:

  1. call ((MyUITableViewCell*)slider.superview).label (if the slider is a direct subview of your cell) 调用((MyUITableViewCell *)slider.superview).label(如果滑块是单元格的直接子视图)
  2. at very first touch, remember the right cell in a global variable (because here you recreate the variable every time) 第一次接触时,请记住全局变量中的正确单元格(因为在这里您每次都会重新创建变量)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM