我想要一些像下面的图片。用户可以单击+或 - 按钮,增加或减少的计数显示在UILabel中,即下图中的1 我知道如何实现自定义表视图单元格。我也知道如何实现选择器。但我需要根据点击的按钮设置标签文本,这只是问题所在。 如何更新按钮点击和设置自定义单元格的文本 from cellForRowAtIndexPathwith 添加减去功能。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
static NSString *simpleTableIdentifier = @"ProductListVIICell";
ProductListVIICell *cell = (ProductListVIICell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"ProductListVIICell" owner:self options:nil];
cell = [nib objectAtIndex:0];
} ....................................
cell.btnMinus.tag = indexPath.row;
cell.btnPlus.tag = indexPath.row;
[cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
cell.lblCount.tag = [[NSString stringWithFormat:@"%d%d",addBtnClickCount,addBtnClickCount] integerValue];
return cell;
}
我需要在这里做一些事情,但没有得到我想要实现的目标。 cell.lblTitle.text
无效
#pragma mark - UIButton selector
-(void)addItem:(UIButton*)button {
itemCount++;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.lblCount.textColor = [UIColor redColor];
cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"%d",itemCount);
[self.tableView reloadData];
}
-(void)deleteItem:(UIButton*)button {
itemCount--;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:button.tag inSection:1];
ProductListVIICell *cell = (ProductListVIICell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.lblCount.textColor = [UIColor redColor];
cell.lblTitle.text = [NSString stringWithFormat:@"%d",itemCount];
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
NSLog(@"%d",itemCount);
[self.tableView reloadData];
}
这些链接也很有用
Accessing cell attributes outside of cellForRowAtIndexPath
http://code.tutsplus.com/tutorials/blocks-and-table-view-cells-on-ios--mobile-22982 但它仅适用于UITableViewCell而不是自定义单元格
我能够使用以下
解决我的问题@interface ProductListVC ()
{
int count;
bool addBtnClicked;
bool minusBtnClicked;
}
@property (strong,nonatomic) NSMutableArray* quantityArray; //stores label
@property (strong,nonatomic) NSMutableArray* quantityLabelArray; //stores label
- (void)viewDidLoad {
[super viewDidLoad];
_quantityLabelArray = [[NSMutableArray alloc] init];
_quantityArray = [[NSMutableArray alloc] init];
//determine num of row prevent index 2 beyond bounds for empty array error i.e insert straight to let's say index 3
for (int i=0; i <=[_productArray count]; i++) {
[_quantityArray addObject:@"0"];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
................
if (!_quantityArray || !_quantityArray.count){
cell.lblCount.text = [NSString stringWithFormat:@"%d", 0];
}else{
cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
}
[_quantityLabelArray addObject:cell.lblCount];
if (addBtnClicked || minusBtnClicked) {
//If add or minu button is reloading the cell
cell.lblCount.text = [_quantityArray objectAtIndex:indexPath.row];
//NSLog(@"%@",cell.lblCount.text);
[_quantityLabelArray replaceObjectAtIndex:indexPath.row withObject:cell.lblCount];
addBtnClicked = NO;
minusBtnClicked = NO;
}
else
{
}
cell.btnMinus.tag = indexPath.row;
cell.btnPlus.tag = indexPath.row;
cell.lblCount.tag = indexPath.row;
[cell.btnPlus addTarget:self action:@selector(addItem:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnMinus addTarget:self action:@selector(deleteItem:) forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}
#pragma mark - UIButton selector
-(void)addItem:(UIButton*)button
{
NSLog(@"btn tag %ld",(long)[button tag]);
NSLog(@"quantitylblarry %@",_quantityLabelArray);
UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];
if ([lblShowCount.text integerValue]< 10) {
count = count+1;
lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]+1];
}
NSLog(@"quantity array count %lu",(unsigned long)_quantityArray.count);
if (_quantityArray.count > [button tag] ) {
[_quantityArray removeObjectAtIndex:[button tag]];
[_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
}
else{
[_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
}
addBtnClicked = YES;
NSLog(@"quantity array %@",_quantityArray);
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
-(void)deleteItem:(UIButton*)button{
NSLog(@"%ld",(long)[button tag]);
UILabel* lblShowCount = [_quantityLabelArray objectAtIndex:[button tag]];
if ([lblShowCount.text integerValue]>=1) {
count = count-1;
lblShowCount.text = [NSString stringWithFormat:@"%ld", [lblShowCount.text integerValue]-1];
}
NSLog(@"%lu",(unsigned long)_quantityArray.count);
if (_quantityArray.count > [button tag] ) {
[_quantityArray removeObjectAtIndex:[button tag]];
[_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
}
else{
[_quantityArray insertObject:lblShowCount.text atIndex:[button tag]];
}
NSLog(@"%@",_quantityArray);
minusBtnClicked =YES;
NSLog(@"quantity array %@",_quantityArray);
NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[button tag] inSection:0];
NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
[self.tableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationNone];
}
答案 0 :(得分:2)
最好使用自定义单元格。选中此tutorials或此one,然后您需要将委托添加到此单元格,以便您可以与ViewController同步数量
即
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
cell.delegate = self;
cell.itemIndex = indexPath.row;
....
return cell
}
<强>更新强>
如果您有自定义单元格,则可以添加UILabel
商店并在+/-
按钮的操作中修改其文字,所有这些代码都在CustomTableViewCell.m
-(void)addItem:(UIButton*)button {
self.itemQuantity++;
[self updateQuantity];
}
-(void)deleteItem:(UIButton*)button {
self.itemQuantity--;
[self updateQuantity];
}
- (void)updateQuantity{
self.lblCount.text = [NSStirng stringWithFormat:@"%d", self.itemQuantity];
[self.delegate updateItemAtIndex:self.itemIndex withQuantity:self.itemQuantity];
}
更新:完整解决方案
<强> 1。模型强>
@interface Item : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic) NSInteger quantity;
@end
@implementation Item
@end
<强> 2。自定义单元格
@interface CustomItemTableViewCell : UITableViewCell
@property (nonatomic, weak) IBOutlet UILabel *lblTitle;
@property (nonatomic, weak) IBOutlet UILabel *lblCount;
@property (nonatomic, assign) Item *item;
@end
@implementation CustomItemTableViewCell
- (void)updateCellWithItem:(Item *)item {
self.item = item;
[self updateCellView];
}
- (void)updateCellView {
self.lblTitle.text = self.item.title;
self.lblTitle.text = [NSString stringWithFormat:@"%ld", self.item.quantity];
}
- (IBAction)addItem:(UIButton*)button {
self.item.quantity++;
[self updateCellView];
}
- (IBAction)deleteItem:(UIButton*)button {
self.item.quantity--;
[self updateCellView];
}
@end
第3。 TableViewController 强>
@interface ItemsTableViewController : UITableViewController
@property (nonatomic, strong) NSArray *items;
@end
@implementation ItemsTableViewController
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomItemTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomItemCell" forIndexPath:indexPath];
[cell updateCellWithItem:self.items[indexPath.row]];
return cell;
}
答案 1 :(得分:0)
要更改单元格标签文本,您需要在这些选择器中提取单元格对象,您只需使用sender参数即可,例如按钮。请参阅以下代码以添加功能。
-(void)addItem:(UIButton*) button
{
NSLog(@"UIButton%ld",(long)button.tag);
addBtnClickCount++; //Increase the count by 1
MyCustomCell *cell = button.superview.superview;
//Now change the label text for cell
cell.lblCount.text = [NSString stringWithFormat:@"%d",addBtnClickCount];
}
您可以采用与减法功能相同的方式。
答案 2 :(得分:0)
在自定义tableview单元格中使用更陡峭的控件。
this is my screen.i used stepper control and label.
customcell.h class
@property (weak, nonatomic) IBOutlet UIStepper *stepper;
@property (weak, nonatomic) IBOutlet UILabel *lbl_value;
**** **** customcell.m
- (IBAction)stepper:(UIStepper *)sender {
NSUInteger value = sender.value;
lbl_value.text = [NSString stringWithFormat:@"%03d",value];
self.txt1.text= [NSString stringWithFormat:@"%03d",value];
}