UIslider价值问题

| 我在表格视图中有一个滑块。在滑块的动作方法中,我做了一些计算并重新加载了表格。当表格第二次重新加载时,它将滑块值设置为其最小值。 谁能知道为什么会发生这个奇怪的问题。 提前致谢! 码:
- (IBAction)sliderAction:(id)sender
{
    UISlider* durationSlider = sender;
    float gmval,finalCalculationValue;  
    float tempgmValue=[self.gmValue floatValue];
    gmval=durationSlider.value;
    tempgmValue=gmval/tempgmValue;
    finalCalculationValue=tempgmValue/100;
    tempgmValue=finalCalculationValue*[self.gmValue floatValue];
    self.gmValue=[NSString stringWithFormat:@\"%0.f\",tempgmValue];
    self.label.text=[NSString stringWithFormat:@\"%f\",tempgmValue];
    [self.tableView reloadData];
}

- (UITableViewCell *)tableView:(UITableView *)tbleView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @\"MyIdentifier\";
                                            UITableViewCell *cell;

UILabel *label                              


cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if(cell == nil){
cell = [self reuseTableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath];
}
    return cell;
}
                            }
-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath {
CGRect cellRectangle;
cellRectangle = CGRectMake(0.0, 0.0, 320, 100);

UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:identifier] autorelease];
UILabel *label1;                    
cellRectangle = CGRectMake(15.0, 50, 200, 20.0);
label1 = [[UILabel alloc] initWithFrame:cellRectangle];
label1.tag = 5;                             label1.textAlignment = UITextAlignmentCenter;
[label1 setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:label1];
[label1 release];                                   


UISlider *gmSlider=[[UISlider alloc]initWithFrame:CGRectMake(13, 20, 180, 21)];    gmSlider.tag=2003;                                     gmSlider.backgroundColor = [UIColor clearColor];  

   gmSlider.continuous = YES;
    gmSlider.minimumValue=10;
                                gmSlider.maximumValue=200;                              gmSlider.value=@\"100\";                                  [gmSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];  
    [cell.contentView addSubview:gmSlider];
    }
    return cell;                                 
    }                                
    
已邀请:
我改变了您的方法...似乎对我来说很好用...在您身边检查...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *MyIdentifier = @\"MyIdentifier\";
    UITableViewCell *cell;

    UILabel *label                              ;
    CGRect cellRectangle;
    cellRectangle = CGRectMake(0.0, 0.0, 320, 100);


    cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell == nil){
        cell = [self reuseTableViewCellWithIdentifier:MyIdentifier withIndexPath:indexPath];
    }
    for (UIView * view in cell.contentView.subviews) {
        [view removeFromSuperview];
        view = nil;
    }
    UILabel *label1;                    
    cellRectangle = CGRectMake(15.0, 50, 200, 20.0);
    label1 = [[UILabel alloc] initWithFrame:cellRectangle];
    label1.tag = 5;                             label1.textAlignment = UITextAlignmentCenter;
    [label1 setBackgroundColor:[UIColor clearColor]];
    [cell.contentView addSubview:label1];
    [label1 release];                                   
    UISlider *gmSlider=[[UISlider alloc]initWithFrame:CGRectMake(13, 20, 180, 21)];    gmSlider.tag=2003;                                     gmSlider.backgroundColor = [UIColor clearColor];  

    gmSlider.continuous = YES;
    gmSlider.minimumValue=10;
    gmSlider.maximumValue=200;                              
    gmSlider.value=100.0;                                  
    [gmSlider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];  
    [cell.contentView addSubview:gmSlider];

    return cell;
}

-(UITableViewCell *)reuseTableViewCellWithIdentifier:(NSString *)identifier withIndexPath:(NSIndexPath *)indexPath {
    CGRect cellRectangle;
    cellRectangle = CGRectMake(0.0, 0.0, 320, 100);

    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:cellRectangle reuseIdentifier:identifier] autorelease];



return cell;                                 
}               
    
如果显示-tableView:cellForRowAtIndexPath:的代码会有所帮助,但是有根据的猜测是该方法存在麻烦。人们通常会忘记,只要单元格从屏幕上滚动下来,它就可以重新使用。同样,由于单元被重用,因此每次都必须在-tableView:cellForRowAtIndexPath:中对其进行配置。该方法应如下所示:
- (UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:someIdentifier];
    if (cell == nil) {
        cell = [[MyTableViewCell alloc] init...];
        [cell autorelease];  // autorelease here to balance alloc
        // Don\'t set the cell up in here or you\'ll have problems!
    }
    // Set the cell up here so that you handle both the new cell and the reuse cases.
    cell.sliderValue = [self sliderValueAtIndex:indexPath.row];
    cell.label = [self labelForValueAtIndex:indexPath.row];
    // ...and so on....

    return cell;
}
    
问题在于,每次单元格重新加载
gmSlider.value=@\"100\";
时,您都要设置滑块的值。您已将该值设置为新计算的值,而不是常数100。     

要回复问题请先登录注册