我要创建一种视图,在其中我有一个自定义单元格以显示项目列表以及一个用于添加和删除项目的按钮(+/-)。
当我单击添加或删除按钮时,我希望它仅对一个单元格起作用,并增加该单元格的值。
现在,它可以工作,但是会增加单元中的所有内容,如何为每个独立的单元管理呢?
- (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];
}
-(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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句