获取 UITableView 中多个 UISlider 的值
我有一个 UITableView,每行都有 UISliders。它们都调用 -(void) sliderValueChanged: (id)sender 方法。
然后,我在 UITableView 外部有一个标签,用 UISlider 的值减去 Incomes 的 CoreData 值填充该标签。问题是如何从总收入值中去掉总滑块值。这是我到目前为止所拥有的,但我存储在 NSNumber previousNumber 变量中的滑块值无法正常工作。任何帮助都会很棒。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISlider *theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(55,20,220,45)] autorelease];
theSlider.maximumValue=[self.totalIncomes floatValue];
theSlider.minimumValue=0;
[theSlider addTarget:self action: @selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:theSlider];
}
NSInteger section = [indexPath section];
Categories *category = (Categories *)[categoriesArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[category name]];
[category release];
return cell;
}
-(void) sliderValueChanged: (id)sender
{
UISlider *control = (UISlider *) sender;
float value = control.value;
float total = [self.totalIncomes floatValue];
if ([self.previousNumber floatValue] < value) {
total = total - (value - [self.previousNumber floatValue]);
}
else {
total = total + ([self.previousNumber floatValue] - value);
}
self.totalIncomes = [NSNumber numberWithFloat: (float)total];
NSLog(@"%.2f", [self.totalIncomes floatValue]);
label.text = [NSString stringWithFormat:@"%.2f",[self.totalIncomes floatValue]];
self.previousNumber = [NSNumber numberWithFloat:(float)value];
//[self.tableView reloadData];
}
I have a UITableView with UISliders on each row. They all call the -(void) sliderValueChanged: (id)sender method.
I then have a label outside the UITableView that I populate with the value of the UISlider minus the CoreData value of Incomes. The problem is how do I take away the total slider values from the total income value. This is what I have so far but the slider value which I store in the NSNumber previousNumber variable does not work properly. Any help would great.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISlider *theSlider = [[[UISlider alloc] initWithFrame:CGRectMake(55,20,220,45)] autorelease];
theSlider.maximumValue=[self.totalIncomes floatValue];
theSlider.minimumValue=0;
[theSlider addTarget:self action: @selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
[cell addSubview:theSlider];
}
NSInteger section = [indexPath section];
Categories *category = (Categories *)[categoriesArray objectAtIndex:[indexPath row]];
cell.textLabel.text = [NSString stringWithFormat:@"%@",[category name]];
[category release];
return cell;
}
-(void) sliderValueChanged: (id)sender
{
UISlider *control = (UISlider *) sender;
float value = control.value;
float total = [self.totalIncomes floatValue];
if ([self.previousNumber floatValue] < value) {
total = total - (value - [self.previousNumber floatValue]);
}
else {
total = total + ([self.previousNumber floatValue] - value);
}
self.totalIncomes = [NSNumber numberWithFloat: (float)total];
NSLog(@"%.2f", [self.totalIncomes floatValue]);
label.text = [NSString stringWithFormat:@"%.2f",[self.totalIncomes floatValue]];
self.previousNumber = [NSNumber numberWithFloat:(float)value];
//[self.tableView reloadData];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论