返回视图时 iOS UISlider 旋钮重影

发布于 2024-12-12 21:43:28 字数 761 浏览 1 评论 0原文

在 tableviewcell 中我有一个 UISlider。如果我移动滑块旋钮,返回到之前的视图,然后返回到表格视图,滑块上的旋钮将返回到零,但我会看到我之前移动滑块的旋钮出现“重影” 。

我清除 cellForRowAtIndexPath: 中滑块对象的上下文视图,并在 viewDidAppear 中重新加载表格。

有人知道如何解决这个问题吗?这很烦人。如果有帮助的话,我将滑块代码放在下面。

        // Setup slider
        CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
        UISlider *slider = [[UISlider alloc] initWithFrame:sliderFrame];
        slider.clearsContextBeforeDrawing = YES;
        [slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
        [slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:slider];
        [slider release];

我很感激!谢谢!

In a tableviewcell I have a UISlider. If I move the slider knob, go back to my previous view, and then return back to the table view, the knob on the slider returned back to zero but I see a "ghosting" of the knob where I had previously moved the slider too.

I clear the context view on the slider object in cellForRowAtIndexPath: and reload the table in viewDidAppear.

Anyone know how to fix this? It's quite annoying. I put the slider code down below if that helps at all.

        // Setup slider
        CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
        UISlider *slider = [[UISlider alloc] initWithFrame:sliderFrame];
        slider.clearsContextBeforeDrawing = YES;
        [slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
        [slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:slider];
        [slider release];

I appreciate it! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

書生途 2024-12-19 21:43:29

您很可能正在旧的 UISlider 上创建一个新的 UISlider,从而产生重影效果。

两种可能的解决方案

  1. UISlider 添加到单元格时对其进行标记。

  2. 子类 UITableViewCell 并将 UISlider 添加到其内容视图,并使用 ivar 保留对其的引用。

要执行的操作 1,只需在将 UISlider 添加到 contentView 时对其进行标记即可。然后,当您再次获得单元格时,您首先尝试恢复视图,或者重新创建它。

const int sliderViewTag = 99;

UISlider *slider = [cell.contentView viewWithTag:sliderViewTag];

if (!slider) {
  CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
  slider = [[UISlider alloc] initWithFrame:sliderFrame];
  slider.clearsContextBeforeDrawing = YES;
  [slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
  [slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
  [cell.contentView addSubview:slider];
  [slider release]; slider = nil;
}

虽然 2 有点复杂,但它是我的首选方法,但我确信有一些很好的例子可以说明如何做到这一点。 Apple 有一些很棒的文档,因此请查看它们 Table View 编程指南 具体看一下 A Closer Look at Table-View 部分细胞

It is most likely that you are creating a new UISlider over the old UISlider which gives the ghosting effect.

Two possible solutions

  1. Tag the UISlider when you add it to a cell.

  2. Subclass UITableViewCell and add a UISlider to it's content view and keep a reference to it with an ivar.

To do 1 simply tag the UISlider when you add it to the contentView. Then when you get a cell again you try getting the view back first or you create it a fresh.

const int sliderViewTag = 99;

UISlider *slider = [cell.contentView viewWithTag:sliderViewTag];

if (!slider) {
  CGRect sliderFrame = CGRectMake(15, 56, 230, 0);
  slider = [[UISlider alloc] initWithFrame:sliderFrame];
  slider.clearsContextBeforeDrawing = YES;
  [slider addTarget:self action:@selector(sliderUpdated:) forControlEvents:UIControlEventValueChanged];
  [slider addTarget:self action:@selector(sliderStopped:) forControlEvents:UIControlEventTouchUpInside];
  [cell.contentView addSubview:slider];
  [slider release]; slider = nil;
}

Although 2 is a little more involved it is my preferred method but I am sure there are some great examples of how to do it. There is some great docs by Apple so check them out Table View Programming Guide specifically look at the section A Closer Look at Table-View Cells

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文