2014-12-06 36 views
-1

我正在开发一个购物应用程序,其中我正在实施购物车。在我的应用程序中,当单击加号按钮时,我需要增加产品数量,单击减号按钮时减少产品数量。在这里我的问题是,当我点击加号按钮时,所有文本字段值在tableviewcell中发生变化。帮帮我吧,下面是 加号按钮操作方法如何在uitableviewcell中点击按钮时更改文本框的值iOS

-(IBAction)plusBtn:(UIButton*)plus 
{ 
    [self.tbleView beginUpdates]; 

    UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview]; 
    NSIndexPath *clickedButtonIndexPath = [self.tbleView indexPathForCell:clickedCell]; 

    plus.tag = clickedButtonIndexPath.row; 
    quantity.tag = clickedButtonIndexPath.row; 

    _curr =_curr+1; 
    quantity.text = [NSString stringWithFormat:@"%d",_curr]; 

    [self.tbleView endUpdates]; 
    [self.tbleView reloadData]; 
} 

这样

enter image description here

越来越..你的按钮

enter image description here

+0

请添加您的'cellForRowAtIndexPath'和plusButton操作方法 – Lalji 2014-12-06 10:57:23

+0

的代码,我已经添加的方法现在 – iOSDevp 2014-12-06 11:05:36

+0

你有一个自定义单元格或您正在使用[细胞viewWithTag:XX]添加文本到文本框的数量? – 2014-12-06 11:12:53

回答

2

您可以检查此功能。它会对你有用。我希望它。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    quantity =[NSMutableArray new]; 
    occu_list = [[NSArray alloc] initWithObjects:@"Occupation", @"two", @"three", @"four", @"five", @"six", @"seven", @"eight", nil]; 
    for(int i=0;i<[occu_list count];i++) 
    { 
     [quantity addObject:@"0"]; 
    } 
    click_textView=[[UIView alloc]init]; 
    click_textView.frame=self.view.frame; 
    [self.view addSubview:click_textView]; 
    [self tableviewone]; 
} 

-(void)tableviewone 
{ 
    tbl_view = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain]; 
    tbl_view.delegate = self; 
    tbl_view.dataSource = self; 
    tbl_view.backgroundColor = [UIColor whiteColor]; 
    tbl_view.layer.borderColor=[[UIColor orangeColor]CGColor]; 
    tbl_view.layer.borderWidth=2.0f; 
    tbl_view.layer.cornerRadius=5.0f; 
    tbl_view.layer.masksToBounds=YES; 
    [click_textView addSubview:tbl_view]; 
} 

-(IBAction)check_btn_action:(id)sender 
{ 
    UIButton *btntag=(UIButton*)sender; 

    NSLog(@"%li",(long)btntag.tag); 
    NSLog(@"%@",[quantity objectAtIndex:btntag.tag]); 
    int ad=[[quantity objectAtIndex:btntag.tag]integerValue]; 
    ad=ad+1; 
    [quantity removeObjectAtIndex:btntag.tag]; 
    [quantity insertObject:[NSString stringWithFormat:@"%i",ad] atIndex:btntag.tag]; 
    [tbl_view reloadData]; 
} 
1

写入操作方法,然后那里面只需使用这一行代码将您的值设置为文本字段即可: -

[email protected]"yourString" 
+0

感谢您的回答,这不是我的问题,在这里我有textfileld和uitableviewcell中的加号和减号按钮,我想改变文本域的值,当加号或减号按钮被点击 – iOSDevp 2014-12-06 10:43:47

+1

ROFL多次 – Andy 2014-12-06 11:12:52

1

唐`写在视图控制器按钮动作,

它写在CustomCell类。

例如:

在CustomCell.m

- (IBAction)plusButtonDidClicked:(UIButton *)sender { 

    int i = [self.textField.text intValue]; 

    i++; 

    self.textField.text = [NSString stringWithFormat:@"%d", i]; 
} 

然后回到ViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath { 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier]; 

    return cell; 
} 

这将是工作做好

0
-(IBAction)plusBtn:(UIButton*)plus 
{ 
    UITableViewCell *clickedCell = (UITableViewCell *)[[plus superview] superview]; 
    _curr=_curr+1; 
    UITextField *qty = (UITextField *) [clickedCell viewWithTag:90]; 
    qty.text=[NSString stringWithFormat:@"%d",_curr]; 
} 
相关问题