2011-12-13 15 views
0

我想改變tableviewcell的加號和減號按鈕點擊項目按鈕值。 這是我的行索引方法代碼的tableview單元格。如何處理和檢測iPhone中的tableviewcell甚至多個按鈕

static NSString *CellIdentifier = @"Cell"; 

      CustomCell *cell = [menuTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
      if (cell == nil) { 
       cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
      } 

      UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      minusBtn.frame= CGRectMake(400, 5,30,30); 
      minusBtn.tag = indexPath.row; 
      [minusBtn setImage:[UIImage imageNamed:@"removeButan.png"] forState:UIControlStateNormal]; 
      [minusBtn addTarget:self action:@selector(minusAction:) forControlEvents:UIControlEventTouchUpInside]; 

      UIButton *plusBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      plusBtn.frame= CGRectMake(500, 5,30,30); 
      plusBtn.tag = indexPath.row; 
      [plusBtn setImage:[UIImage imageNamed:@"Addbutton.png"] forState:UIControlStateNormal]; 
      [plusBtn addTarget:self action:@selector(plusAction:) forControlEvents:UIControlEventTouchUpInside];  

      UIButton *itemBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      itemBtn.frame= CGRectMake(450, 5,30,30); 
      itemBtn.tag = indexPath.row; 
      [itemBtn setBackgroundImage:[UIImage imageNamed:@"number_butan_1.png"] forState:UIControlStateNormal]; 
      [itemBtn setTitle:totalItem forState:UIControlStateNormal]; 
      [itemBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 

      UIButton *addToCart = [UIButton buttonWithType:UIButtonTypeCustom]; 
      addToCart.frame= CGRectMake(580,5,30,30); 
      addToCart.tag = indexPath.row; 
      addToCart.font = [UIFont systemFontOfSize:14]; 
      [addToCart setImage:[UIImage imageNamed:@"cart.png"] forState:UIControlStateNormal]; 
      [addToCart addTarget:self action:@selector(addToCartAction:) forControlEvents:UIControlEventTouchUpInside]; 

      UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
      infoBtn.frame= CGRectMake(630, 5,30,30); 
      infoBtn.tag = indexPath.row; 
      [infoBtn setImage:[UIImage imageNamed:@"info.png"] forState:UIControlStateNormal]; 
      [infoBtn addTarget:self action:@selector(displayDetailView:) forControlEvents:UIControlEventTouchUpInside]; 

      // lblTitle.text = [nameArray objectAtIndex:indexPath.row]; 
      cell.primaryLabel.text = [appDelegate.nameArray objectAtIndex:indexPath.row]; 
      cell.secondaryLabel.text = [appDelegate.sortDetailArray objectAtIndex:indexPath.row]; 
      cell.priceLabel.text = [appDelegate.rateArray objectAtIndex:indexPath.row]; 

      [cell.contentView addSubview:minusBtn]; 
      [cell.contentView addSubview:plusBtn]; 
      [cell.contentView addSubview:addToCart]; 
      [cell.contentView addSubview:itemBtn]; 
      [cell.contentView addSubview:infoBtn]; 

      return cell; 
} 


- (IBAction)plusAction:(id)sender{ 

    // Hat* obj = ((UIButton *)sender).tag; 
    // NSNumber* hatId = obj.idValue; 

    UITableViewCell *cell=(UITableViewCell*)[[sender superview] superview]; 
    UITableView *table=(UITableView*)[cell superview]; 
    NSIndexPath *path=[table indexPathForCell:cell]; 

    int value = [totalItem intValue]; 
    value++; 

    totalItem = [NSString stringWithFormat:@"%d",value]; 
    NSLog([NSString stringWithFormat:@"%@",totalItem]); 
    [menuTableView reloadData]; 

} 

- (IBAction)minusAction:(id)sender{ 

    UIView *contentView = [sender superview]; 

    UITableViewCell *cell=(UITableViewCell*)[[contentView superview] superview]; 
    UITableView *table=(UITableView*)[cell superview]; 
    NSIndexPath *path=[table indexPathForCell:cell]; 

    NSUInteger buttonRow = [[menuTableView 
          indexPathForCell:cell] row]; 

    int value = [totalItem intValue]; 
    value--; 

    totalItem = [NSString stringWithFormat:@"%d",value]; 
    NSLog([NSString stringWithFormat:@"%@",totalItem]); 
    [menuTableView reloadData]; 

} 

我將嘗試這一點,但不是working.it改變所有行值

+2

不給相同的標籤給所有的按鈕給標籤,如100 + indexPath.row,200 + indexPath.row,.. – Leena

回答

1

你只有totalItem的一個實例。如果你有很多行,並且你想單獨存儲每一行​​,你將需要創建一個數組來存儲每個單獨的行,然後在cellForRow方法中顯示適當的值。

0

我已經完成了。

- (IBAction)plusAction:(id)sender{ 
    int correctIndex = [sender tag]; 
    int old = [[appDelegate.quantityArray objectAtIndex:correctIndex] intValue]; 
    [appDelegate.quantityArray replaceObjectAtIndex:correctIndex withObject:[NSNumber numberWithInt:old + 1]]; 
    [menuTableView reloadData]; 
} 

- (IBAction)minusAction:(id)sender{ 
    int correctIndex = [sender tag]; 
    int old = [[appDelegate.quantityArray objectAtIndex:correctIndex] intValue]; 
    [appDelegate.quantityArray replaceObjectAtIndex:correctIndex withObject:[NSNumber numberWithInt:old - 1]]; 
    [menuTableView reloadData]; 
} 
相關問題