increment,decrement on label when click the + or - button in ios
ios, uitableview
Solution
-(IBAction)addButton:(id)sender{
TableViewCell *cell;
// check device version
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")){
cell = (TableViewCell*)((UIButton*)sender).superview.superview.superview;
}
else{
cell = (TableViewCell*)((UIButton*)sender).superview.superview.superview.superview;
}
cell.lblNumber.text = [NSString stringWithFormat:@"%d",[cell.lblNumber.text intValue]+1];
}
macro
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
Problem
I want to make a type of view where I have a custom cell to show the item list and a button to add and remove items (+/-). When I click the add or remove button I want it to work for only one cell and increase the value of the cell. For now, it works but it increases everything in the cell, how can I manage this for each independent cell ? ``` - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TableViewCell *cell = (TableViewCell *)[_tableView dequeueReusableCellWithIdentifier:@"Cell"]; cell.addButton.tag = indexPath.row; cell.removeButton.tag = indexPath.row; cell.itemName.text = [models objectAtIndex:indexPath.row]; cell.itemPrice.text = [[stock objectAtIndex:indexPath.row] stringValue]; cell.count = [NSNumber numberWithInt:i]; cell.quantityLbl.text = [NSString stringWithFormat:@"%@", cell.count]; cell.quantityLbl.layer.cornerRadius = YES; cell.quantityLbl.layer.borderWidth = 1.0; return cell; } ``` On add Button:- ``` - (IBAction)addButton:(id)sender { NSInteger num = [sender tag]; i = i + 1; NSIndexPath *rowToReload = [NSIndexPath indexPathForRow:num inSection:0]; NSArray *rowsToReload = [NSArray arrayWithObjects:rowToReload, nil]; [_tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone]; } ```