单击 ios 中的 + 或 - 按钮时标签上的递增、递减
increment,decrement on label when click the + or - button in ios
我想制作一种视图,其中我有一个自定义单元格来显示项目列表和一个按钮来添加和删除项目 (+/-)。
当我单击添加或删除按钮时,我希望它只对一个单元格起作用并增加该单元格的值。
目前,它可以工作,但它会增加单元格中的所有内容,我该如何为每个独立的单元格管理它?
- (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;
}
在添加按钮时:-
- (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];
}
你需要创建另一个数组,称为 "count" 之类的,并将其初始化为与你的 "models" 数组相同的长度,该数组最初有几个整数值为 0 的 NSNumber 对象.
然后,将您的添加按钮代码更改为如下所示:
- (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];
}
并确保您也更新了 cellforrowatindexpath 函数:
- (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;
}
如果您只更新 1 个单元格,请使用此选项:-
[NSArray arrayWithObject:rowToReload];
而不是:-
NSArray *rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
试试这个
- (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];
}
-(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)
我想制作一种视图,其中我有一个自定义单元格来显示项目列表和一个按钮来添加和删除项目 (+/-)。
当我单击添加或删除按钮时,我希望它只对一个单元格起作用并增加该单元格的值。
目前,它可以工作,但它会增加单元格中的所有内容,我该如何为每个独立的单元格管理它?
- (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;
}
在添加按钮时:-
- (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];
}
你需要创建另一个数组,称为 "count" 之类的,并将其初始化为与你的 "models" 数组相同的长度,该数组最初有几个整数值为 0 的 NSNumber 对象.
然后,将您的添加按钮代码更改为如下所示:
- (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];
}
并确保您也更新了 cellforrowatindexpath 函数:
- (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;
}
如果您只更新 1 个单元格,请使用此选项:-
[NSArray arrayWithObject:rowToReload];
而不是:-
NSArray *rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
试试这个
- (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];
}
-(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)