2014-10-08 17 views
0

当我在TableView中滚动时,第2行中的UISlider被奇怪地移动到另一个单元格。Xcode UISlider在滚动后被添加到另一个单元格中

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsTickerFilterCell" forIndexPath:indexPath]; 

...

else if (indexPath.row == 2) { 

     UISlider *radiusSlider = (UISlider *)[cell.contentView viewWithTag:20]; 
     if (!radiusSlider) { 

      // ... noch nicht erstellt 
      radiusSlider = [[UISlider alloc] initWithFrame:CGRectMake(screenWidth - sliderWidth - 70, 0, sliderWidth, 44)]; 
      [radiusSlider setMinimumValue:1]; 
      [radiusSlider setMaximumValue:50]; 
      [radiusSlider setValue:25]; 
      [radiusSlider setBackgroundColor:[UIColor clearColor]]; 
      [radiusSlider setTag:20]; // zur Erkennung, ob die View schon erstellt wurde 
      [radiusSlider addTarget:self action:@selector(radiusSliderChangedValue:) forControlEvents:UIControlEventValueChanged]; 
      [cell.contentView addSubview:radiusSlider]; 
     } 

    } 

我怎样才能制止这种行为?

在此先感谢 克里斯

+1

一旦发送了行整个小区在索引代码处。并且重要的一点是,如果( == nil) { – 2014-10-08 10:22:20

回答

1

这不会被移动到另一个单元格。这是因为细胞出列

这条线......

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsTickerFilterCell" forIndexPath:indexPath]; 

检查可用单元的高速缓存,并返回一个(或创建一个新的,如果没有一个可用)。当细胞离开屏幕时,它们将被重新使用。这意味着您可以拥有一个包含数千个单元的表格,而不会耗尽内存。

无论如何,要解决它,你需要使用这个。你已经在使用viewWithTag,所以我会坚持,但最好有一个自定义UITableViewCell子类来做到这一点。

所以......

// I will ignore the else for demonstration purposes because I don't know what came before it 
if (indexPath.row == 2) { 

    UISlider *radiusSlider = (UISlider *)[cell.contentView viewWithTag:20]; 
    if (!radiusSlider) { 
     // ... noch nicht erstellt 
     radiusSlider = [[UISlider alloc] initWithFrame:CGRectMake(screenWidth - sliderWidth - 70, 0, sliderWidth, 44)]; 
     [radiusSlider setMinimumValue:1]; 
     [radiusSlider setMaximumValue:50]; 
     [radiusSlider setValue:25]; 
     [radiusSlider setBackgroundColor:[UIColor clearColor]]; 
     [radiusSlider setTag:20]; // zur Erkennung, ob die View schon erstellt wurde 
     [radiusSlider addTarget:self action:@selector(radiusSliderChangedValue:) forControlEvents:UIControlEventValueChanged]; 
     [cell.contentView addSubview:radiusSlider]; 
    } 
} else { 
    // remove the slider from the cell if not row 2 
    UISlider *radiusSlider = (UISlider *)[cell.contentView viewWithTag:20]; 
    [radiusSlider removeFromSuperView]; 
} 

使用自定义类

您将有几个这样的方法......

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // register your custom cell class with this reuse identifier in view did load 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsTickerFilterCell" forIndexPath:indexPath]; 

    // this is pretty much all that should happen in here. 
    [self configureSliderCell:(MyCustomSliderCell *)cell atIndexPath:indexPath]; 

    return cell; 
} 

- (void)configureSliderCell:(MyCustomSliderCell *)cell atIndexPath:(NSIndexPath *)indexPath 
{ 
    //other configuration stuff here 

    // the slider will already exist and be in the cell and be an outlet property 
    // it will have the correct frame, min, max, etc... 
    // all you need to do here is hide or show it depending on which row it is. 
    cell.radiusSlider.hidden = indexPath.row != 2; 
} 
+0

我认为最好的解决方案将是这种方法的自定义单元格。其他一切只是一个解决办法。感谢您的回答! – 2014-10-08 10:33:26

+1

@ChristianPappenberger我编辑过,以显示如何使用自定义单元格子类来完成此操作。 – Fogmeister 2014-10-08 10:50:25

+0

非常感谢。我知道了:) – 2014-10-09 09:52:12

0

这是因为iOS的重用细胞。如果您更改单元格,请将其更改为标识符。

1

问题是您要将滑块添加到单元格,但IOS将重用相同的单元格。所以如果你想删除滑块做这样的事情:

if (indexPath.row == 2) { 

    UISlider *radiusSlider = (UISlider *)[cell.contentView viewWithTag:20]; 
    if (!radiusSlider) { 

     // ... noch nicht erstellt 
     radiusSlider = [[UISlider alloc] initWithFrame:CGRectMake(screenWidth - sliderWidth - 70, 0, sliderWidth, 44)]; 
     [radiusSlider setMinimumValue:1]; 
     [radiusSlider setMaximumValue:50]; 
     [radiusSlider setValue:25]; 
     [radiusSlider setBackgroundColor:[UIColor clearColor]]; 
     [radiusSlider setTag:20]; // zur Erkennung, ob die View schon erstellt wurde 
     [radiusSlider addTarget:self action:@selector(radiusSliderChangedValue:) forControlEvents:UIControlEventValueChanged]; 
     [cell.contentView addSubview:radiusSlider]; 
    } 

} 
else { 
    UISlider *radiusSlider = (UISlider *)[cell.contentView viewWithTag:20]; 
    [radiusSlider removeFromSuperview]; 
} 

只要从其他细胞超级查看删除滑块。

编辑

正如下面拉梅什Muthe的评论中提到,该解决方案是不是要做到这一点的最好办法。这只是一个解决办法。我会建议你为你的目的制作一个自定义单元。

+1

此解决方案可能会解决该问题,但这不是正确的,因为每当您滚动表格时滑块再次分配 – 2014-10-08 10:27:02

+0

好吧,我明白了。谢谢你的建议! :) – 2014-10-08 10:32:46

+0

@RameshMuthe我知道。最好的解决方案是使用自定义单元格。这只是解决他的问题。无论如何编辑了我的答案,以便它可以更有帮助。 :) – 2014-10-08 10:33:57

相关问题