I want some thing like image below. User can click on + or - button and the increased or decreased count is display in UILabel i.e 1 in the image below.
I know how to im
use steeper control in custom tableview cell.
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];
}
To change the cell label text you need to extract the cell object in those selectors, which you can do simply by using the sender parameter, in your case a button. See the code below for add functionality.
-(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];
}
Same way you can do for subtract functionality.
it is better to use custom cell. Check this tutorials or this one, then you will need to add delegate to this cell so that you can sync quantities with you ViewController
i.e.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomTableViewCell *cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell" forIndexPath:indexPath];
cell.delegate = self;
cell.itemIndex = indexPath.row;
....
return cell
}
UPDATE
if you have a custom cell, you would add an UILabel
outlet and modify its text in the action of +/-
buttons, all this code will be in 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];
}
UPDATE: COMPLETE SOLUTION
1. Model
@interface Item : NSObject
@property (nonatomic, strong) NSString *title;
@property (nonatomic) NSInteger quantity;
@end
@implementation Item
@end
2. Custom Cell
@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;
}