iOS 计算价格的加减,防止重复添加

//按钮响应方法

-(void)buttonAction:(UIButton *)sender
{

//判断按钮点击状态
    sender.selected = !sender.selected;

//通过状态不同赋不同的图片
    if(sender.selected == NO)
    {
        _kImage.image = [UIImage imageNamed:@""];
    }
    else
    {
        _kImage.image = [UIImage imageNamed:@"选择回报——框"];
    }

//截取字符串所需的部分
    NSString *price = [_priceLabel.text substringFromIndex:1];

//block传值,参数价格,按钮的状态
    self.block(price,sender.selected);
}

//UITableView代理方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CYChooseTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.backgroundColor = [UIColor lightGrayColor];

//单元格取消点击状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //block传值
    cell.block =^(NSString *summ,BOOL isSelected)
    {
        [self judgeprice:summ Selected:isSelected];
    };
    return cell;
}
- (void)judgeprice:(NSString *)price Selected:(BOOL)selected{
    //通过按钮的点击状态判断加减价格
    if (selected) {
        sum += [price doubleValue];
    }else{
        sum -= [price doubleValue];
    }

    NSLog(@"%lf",sum);
    [self.holdButton setTitle:[NSString stringWithFormat:@"支持:¥%.2lf",sum] forState:UIControlStateNormal];
}

posted on 2017-02-17 21:35  Da大龙  阅读(101)  评论(0)    收藏  举报

导航