0

私のアプリでは、テーブルビューにスライダーを追加します。つまり、各行にスライダーが含まれています。これも正常に機能します。

しかし、テーブルビューをスクロールすると、スライダーがリロードされます。つまり、スライダーの値ではなく、開始位置が表示されます。

//My code is as follow for slider in table cell:

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

     NSString *CellIdentifier=[NSString stringWithFormat:@"CellIdentifier%d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

        return cell;
    }


    UISlider*  theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue=99;
        theSlider.minimumValue=0;
        [cell addSubview:theSlider];

return cell;
}

どうすればこの問題を解決できますか?

4

4 に答える 4

2

スライダーの値を配列などに保存し、配列の値に従ってスライダーの値を設定する必要がありますcellforrowatindexpath

于 2013-02-18T10:05:02.387 に答える
0

You can do like this,

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

    NSString *CellIdentifier =[NSString stringWithFormat:@"Cell%d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];

        UISlider*  theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue=99;
        theSlider.minimumValue=0;
        [cell addSubview:theSlider];
    }
    return cell;

}
于 2013-02-18T10:08:01.070 に答える
0
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     NSString *CellIdentifier=[NSString stringWithFormat:@"CellIdentifier%d",indexPath.row];

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISlider*  theSlider = nil;
    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

theSlider     =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
        theSlider.maximumValue=99;
       theSlider.tag =2;
        theSlider.minimumValue=0;
        [[cell contentView] addSubview:theSlider];
    }



return cell;
}
于 2013-02-18T10:16:16.740 に答える
0

より良い答えを得るには、さらにコードが必要です。これで問題が解決しない場合は、全体を投稿してください

-tableView: cellForRowAtIndexPath:

方法。

私の推測では、セルを再利用するたびに新しい Slider を設定している可能性があります

-tableView:dequeueReusableCellWithIdentifier:

これにより、(当然のことながら)毎回開始位置に新しい Slider が作成されます。

再利用されるセルに割り当てることができる辞書またはスライダーの配列を使用する必要があります。

たとえば、TavbleView が 20 行あり、それぞれに 1 つのスライダーが含まれている場合、20 個のスライダーの配列が必要で、次のようにします。

cell.slider = [mySliderArray objectAtIndex: indexPath.row]

そうすれば、スライダーは値を保持します。

編集:これを試してください:(上記のコードを少し変更しました)

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

     NSString *CellIdentifier=[NSString stringWithFormat:@"CellIdentifier%d",indexPath.row];

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

if (cell == nil) {

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];

    return cell;
}

//This is where you went wrong
//UISlider*  theSlider =  [[[UISlider alloc] initWithFrame:CGRectMake(174,12,120,23)] autorelease];
    //theSlider.maximumValue=99;
    //theSlider.minimumValue=0;
    //[cell addSubview:theSlider];

//this is what you need to do:
[cell addSubview: [mySlidersArray objectAtIndex:indexPath.row]];


return cell;
}

明らかに、「mySlidersArray」と呼ばれるスライダーの配列を viewDidload などのどこかに設定する必要があります。

次の編集:スライダーの配列を作成する方法は次のとおりです。

NSMutableArray *arrayOfSliders = [NSMutableArray arrayWithCapacity: 20];
for (int i =0; i<20; i++){
    UISlider *slider = [[UISlider alloc] init];
    //set the slider-properties here, like you already did in your code
    [arrayOfSliders addObject: slider];
}

上記のコードのように、これらのスライダーにアクセスできるようになりました。

もう 1 つの方法は、スライダーの値の配列を保持し、インスタンス化した後にスライダーをそれらの値に設定することです。ただし、画面に表示されるすべてのセルに対して新しいスライダーが作成されるため、パフォーマンスが非常に低下します。

さらにサポートが必要な場合はお知らせください。

于 2013-02-18T10:18:55.613 に答える