UIslider价值问题

时间:2011-05-24 06:21:08

标签: objective-c uitableview ios4 uislider

我在tableview中有一个滑块。在滑块操作方法我做了一些计算并重新获得表。当表重新加载第二次时,它将滑块值设置为其最小值。 任何人都可以知道为什么会发生这个奇怪的问题。 提前谢谢!

代码:

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

3 个答案:

答案 0 :(得分:3)

如果您为-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;
}

答案 1 :(得分:3)

问题是每次单元重新加载gmSlider.value=@"100";时都要设置滑块的值。您已将值设置为新计算的值,而不是常量100。

答案 2 :(得分:3)

我改变了你的方法......似乎对我来说很好......在你身边检查......

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