Objective c 在UITableView中重新加载表子视图

Objective c 在UITableView中重新加载表子视图,objective-c,cocoa-touch,uitableview,Objective C,Cocoa Touch,Uitableview,我有一个tableView,其中添加了一个滑块作为子视图。我尝试用两种方法 方法1: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleV

我有一个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;
}
- (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;
}
问题:如果我使用此选项,那么如果我滚动表格,滑块将重新加载并显示最小值而不是滑块值。所以我使用第二种方法

方法2:

- (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;
}
- (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;
}

问题:我的视图中有一个“下一步”按钮。当我尝试在该按钮上单击时,我希望重新加载所有滑块(即将滑块值设置为最小值)。我试图重新加载表格,但没有得到所需的结果。

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

[[self mytableview] reloadData];

这里,mytableview是您放置table view对象的table view对象。

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

添加实例变量以保存滑块的值:

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];
}

注意:这尚未编译或运行。单击“下一步”按钮时,它可能有输入错误。

。检索tableview中的所有滑块并将其重置为零

for (UISlider *but in [self.view.subviews subviews]) // or use [yourtableview subviews]
    { 
        if ([but isKindOfClass:[UISlider class]]) {
            [but setValue:0.0];
        }
    }

将方法2用于[cell.contentView addSubview:theSlider];和follo Vishal的回答:)您需要为您的滑块设置值。我看不出你在哪里增加价值。您只设置了最小值和最大值。当您使用第二种方法时,滑块是否显示在单元格上?但在何处添加此行?在此行下方:滑块。最小值=0;除了重新加载表(您可能已经知道如何操作),还必须跟踪每个滑块的值,以便为每个单元格设置每个滑块的值。但是按照第二种方法,当我尝试重新加载表时,
(cell==nil)
中的代码没有运行。这很好。在滚动表格时,您仍然需要此代码来跟踪滑块的值。要处理“重新加载”按钮,您需要清除保存的值,然后重新加载表。对不起,我不明白您的意思。我的代码的目的是确保在滚动表格时,滑块值保持在原来的位置。只有在点击“下一步”按钮时才应重置滑块值。是否初始化了
\u sliderValues
?在
initXXX
方法或
viewDidLoad
中,需要
\u sliderValues=[[NSMutableDictionary alloc]init]。是的,您的代码正在工作,但在
ios5