0

我有一个 tableView,我在其中添加了一个滑块作为子视图。我用两种方法试试这个。

方法1:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }

    UISlider*  theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
    theSlider.maximumValue=99;
    theSlider.minimumValue=0;
    [cell.contentView addsubview:theSlider];
    return cell;
}

问题:如果我使用它,那么如果我滚动表格,滑块会重新加载并显示最小值而不是滑块值。所以我使用第二种方法。

方法2:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier=[NSString stringWithFormat:@"CellIdentifier%d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
        UISlider* theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue=99;
        theSlider.minimumValue=0;
        [cell addSubview:theSlider];
    }  
    return cell;
}

问题: 我的视图中有一个下一个按钮。当我尝试点击它时,我想重新加载所有滑块(即将滑块值设置为最小值)。我尝试重新加载表,但我没有得到想要的结果。

4

3 回答 3

2

当用户更改滑块时,您必须跟踪它的最后一个值。然后在 中cellForRowAtIndexPath:,您必须将滑块的值设置为上次保存的值。

添加一个实例变量来保存滑块的值:

NSMutableDictionary *_sliderValues; // don't forget to initialize this variable

// Helper method to located the slider in the cell
- (UISlider *)findSlider:(UIView *)view {
    for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UISlider class]]) {
            return (UISlider *)subview;
        } else {
            UISlider *slider = [self findSlider:subview];
            if (slider) {
                return slider;
            }
    }

    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *CellIdentifier = @"SliderCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 
        UISlider *theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue = 99;
        theSlider.minimumValue = 0;
        [theSlider addTarget:self action:@selector(sliderChanged:) forControlEvents: UIControlEventValueChanged];
        [cell.contentView addSubview:theSlider];
    }

    UISlider *slider = [self findSlider:cell];
    slider.tag = indexPath.row
    slider.value = [_sliderValue[@(slider.tag)] floatValue];

    return cell;
}

// Process changes to a slider
- (void)sliderChanged:(UISlider *)slider {
    NSInteger tag = slider.tag;
    [_sliderValues setObject:@(slider.value) forKey:@(tag)];
}

// called when the Next button is tapped
- (void)nextAction {
    [_sliderValues removeAllObjects];
    [self.tableView reloadData];
}

注意:这尚未编译或运行。它可能有一个错字。

于 2013-03-06T06:03:43.610 回答
0

当您单击下一步按钮时。检索表格视图中的所有滑块并将其重置为零。

for (UISlider *but in [self.view.subviews subviews]) // or use [yourtableview subviews]
    { 
        if ([but isKindOfClass:[UISlider class]]) {
            [but setValue:0.0];
        }
    }
于 2013-03-06T13:18:30.883 回答
0

确定从您的按钮调用此行,它将重新加载您的表格视图您添加的所有数据:

[[self mytableview] reloadData];

在这里,mytableview 是您放置表视图对象的表视图的 obj。

于 2013-03-06T05:46:22.237 回答