Iphone 表视图中的滑块问题
在我的应用程序中,我在表格视图中添加了一个滑块,即每一行都包含一个滑块&它也可以正常工作 但当我滚动表格视图时,滑块会重新加载,即每个滑块都会显示起始位置,而不是滑块值Iphone 表视图中的滑块问题,iphone,ios,objective-c,xcode,uitableview,Iphone,Ios,Objective C,Xcode,Uitableview,在我的应用程序中,我在表格视图中添加了一个滑块,即每一行都包含一个滑块&它也可以正常工作 但当我滚动表格视图时,滑块会重新加载,即每个滑块都会显示起始位置,而不是滑块值 //My code is as follow for slider in table cell: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSS
//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;
}
如何解决此问题???您必须将滑块值存储在数组中等,并根据
单元格中的数组值设置滑块值,以便rowatinexpath
您可以这样做
- (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;
}
我需要更多的代码来获得更好的答案。如果这对你没有帮助,请发布你的全部信息
-tableView: cellForRowAtIndexPath:
方法
我现在的猜测是,每次您使用
-tableView:dequeueReusableCellWithIdentifier:
这(自然)会导致每次在起始位置都有一个新的滑块
您必须使用一个字典或一组滑块,以便分配给正在重用的单元格
例如,如果yout TavbleView应该有20行,每行包含一个滑块,那么您将需要一个包含20个滑块的数组,并执行类似操作
cell.slider=[myslideraray objectAtIndex:indexPath.row]
这样,滑块将保留该值
编辑:试试这个:(我稍微修改了上面的代码)
显然,您需要在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];
}
NSMutableArray*arrayOfSliders=[NSMutableArrayWithCapacity:20];
for(int i=0;我感谢您的回复!但很抱歉,此方法不起作用。滑块值为get reload&显示起点,而不是滑块值。如果(cell==nil){},请在内部创建滑块。然后设置值。谢谢你的回复!但是很抱歉,这个方法不起作用。我如何创建一个包含20个滑块的数组?很明显,你可以创建一个滑块。你在代码中完成了。现在制作20个副本并将其放入一个数组中。你可能需要使用循环。@user1673099我已经用代码将我的答案更新为cr吃滑块阵列。让我知道它是如何工作的
- (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;
}
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];
}