UIslider Value проблема - PullRequest
       19

UIslider Value проблема

0 голосов
/ 24 мая 2011

У меня есть ползунок в табличном представлении. В ползунке Метод действия Я делаю некоторые вычисления и перезагружаю таблицу. Когда таблица перезагружается во второй раз, она устанавливает значение ползунка на минимальное значение.Кто-нибудь может знать, почему эта странная проблема происходит.Заранее спасибо!

Код:

- (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 ]

3 голосов
/ 24 мая 2011

Проблема в том, что вы устанавливаете значение ползунка каждый раз при перезагрузке ячейки gmSlider.value=@"100";.Вы установили значение как новое вычисленное значение, а не как константу 100.

3 голосов
/ 25 мая 2011

Я изменил ваши методы ... и, кажется, работает нормально для меня ... проверьте это на вашей стороне ...

- (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;                                 
}               
3 голосов
/ 24 мая 2011

Было бы полезно, если бы вы показали свой код для -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;
}
...