获取 UITableView 中多个 UISlider 的值

发布于 2024-10-22 05:34:37 字数 2018 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文