这不会被移动到另一个单元格。这是因为细胞出列。
这条线......
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;
}
一旦发送了行整个小区在索引代码处。并且重要的一点是,如果( == nil) { –
2014-10-08 10:22:20
|