UIslider值问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我改变了你的方法...似乎对我来说效果很好...检查一下你的情况...
I changed your methods... and seems to work fine for me... check it on your side...
如果您显示 -tableView:cellForRowAtIndexPath: 的代码会有所帮助,但有根据的猜测是问题出在该方法中。人们常常忘记,一旦单元格滚出屏幕,它就可以重复使用。另外,由于单元格被重用,因此每次都必须在 -tableView:cellForRowAtIndexPath: 中配置它们。该方法应该如下所示:
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:
问题是您每次单元格重新加载时都会设置滑块的值
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.