UIslider值问题

发布于 2024-11-08 23:53:11 字数 2474 浏览 3 评论 0原文

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

代码:

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

I have a slider in tableview.In slider Action method I do some calculation and get Table reloaded.When table reload second time it set the slider value to its minimum value.
Can anybody know why this strange problem is happening.
Thanks in advance!

Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蒗幽 2024-11-15 23:53:11

我改变了你的方法...似乎对我来说效果很好...检查一下你的情况...

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

I changed your methods... and seems to work fine for me... check it on your side...

- (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;                                 
}               
尐偏执 2024-11-15 23:53:11

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

It'd help if you'd show your code for -tableView:cellForRowAtIndexPath:, but an educated guess is that the trouble is in that method. People often forget that as soon as a cell scrolls off the screen, it becomes available for reuse. Also, because cells are reused, they have to be configured each time in -tableView:cellForRowAtIndexPath:. That method should look something like this:

- (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;
}
如果没结果 2024-11-15 23:53:11

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

The problem is that you are setting the value of the slider each time cell reloads gmSlider.value=@"100";. You have set the value as the newly calculated value rather than a constant 100.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文