在UITableView中获取多个UISlider的值

时间:2011-03-16 23:11:16

标签: iphone uitableview uislider

我在每一行都有一个带UISliders的UITableView。它们都调用 - (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];
}

0 个答案:

没有答案