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

方法。

我现在的猜测是,每次使用

-tableView:dequeueReusableCellWithIdentifier:

这(自然)每次都会在起始位置产生一个新的 Slider。

您将不得不使用可以分配给正在重用的单元格的字典或滑块数组。

例如,如果你的 TavbleView 应该有 20 行,每行包含一个滑块,你将需要一个包含 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;
}

显然,您需要在 viewDidload 或其他任何地方设置一个名为“mySlidersArray”的滑块数组。

下一个编辑:这是我创建滑块数组的方法:

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

现在可以像上面的代码一样访问这些滑块。

另一种方法是为滑块保留一组值,并在实例化它们后将滑块设置为这些值。但这会导致性能非常差,因为会为屏幕上出现的每个单元格创建一个新滑块。

如果您需要更多帮助并玩得开心,请告诉我

于 2013-02-18T10:18:55.613 回答
0

你可以这样做,

- (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 回答