Reputation: 1480
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];
}
Upvotes: 3
Views: 5981
Reputation: 10959
-(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];
}
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
Upvotes: 4
Reputation: 2451
try this
- (IBAction)addButton:(id)sender
{
NSInteger num = [sender tag];
i = i + 1;
UIButton * button = (UIButton*)sender;
CGPoint buttonPosition = [button convertPoint:CGPointZero toView: _tableView];
NSIndexPath *indexPathToReload = [_tableView indexPathForRowAtPoint:buttonPosition];
NSArray *rowsToReload = [NSArray arrayWithObjects:indexPathToReload, nil];
[_tableView reloadRowsAtIndexPaths:rowsToReload
withRowAnimation:UITableViewRowAnimationNone];
}
Upvotes: 0
Reputation: 1521
Use this if you are updating only 1 cell:-
[NSArray arrayWithObject:rowToReload];
instead of:-
NSArray *rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
Upvotes: 0
Reputation: 738
You need to make another array, called "count" or something, and initialize it to be the same length as your "models" array, that initially has several NSNumber objects with an integer value of 0.
Then, change your add button code to look like this:
- (IBAction)addButton:(id)sender {
NSInteger num = [sender tag];
int value = [count[num] integerValue];
value += 1;
count[num] = @(value);
NSIndexPath *rowToReload = [NSIndexPath indexPathForRow:num inSection:0];
NSArray *rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[_tableView reloadRowsAtIndexPaths:rowsToReload
withRowAnimation:UITableViewRowAnimationNone];
}
And make sure you update your cellforrowatindexpath function as well:
- (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 = count[indexPath.row];
cell.quantityLbl.text = [NSString stringWithFormat:@"%@", cell.count];
cell.quantityLbl.layer.cornerRadius = YES;
cell.quantityLbl.layer.borderWidth = 1.0;
return cell;
}
Upvotes: 1