3

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 implement custom table view cell .I know how to implement the selector too. but I need to set the label text according as the button clicked thats only is the problem . How can I update button clicked and set text of UILabel of custom cell outside from cellForRowAtIndexPathwith add subtract features.

- (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;
    }

I need to do things here but didnt get what I want to acheive. cell.lblTitle.text isnt working

  #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];

}

enter image description here

These links are useful too

Accessing cell attributes outside of cellForRowAtIndexPath

http://code.tutsplus.com/tutorials/blocks-and-table-view-cells-on-ios--mobile-22982 but it is only for UITableViewCell not the custom cell

I was able to solve my problem using following

@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];

 }
Community
  • 1
  • 1
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57

3 Answers3

2

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;
}
Mahmoud Adam
  • 5,772
  • 5
  • 41
  • 62
0

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.

Gandalf
  • 2,399
  • 2
  • 15
  • 19
  • do u really think it can be done cell.lblCount.text outside from cellForRowAtIndexPath – Nischal Hada Jun 25 '15 at 13:23
  • I haven't tried it myself. But it should as you are just modifying the cell object. Just try it once. – Gandalf Jun 25 '15 at 14:09
  • can u check my modified questions – Nischal Hada Jun 26 '15 at 06:52
  • why are you calculating the indexPath for section 1 in your `addItem:` method. I do not see any section in images or the in the code of `cellForRow...`. Maybe that is the reason. I have tested my logic now and it works, so something else might be in play. Try to use debugger. Print all the cell objects in your table view and then print the cell what you got in `addItem:` method and see if they are same object. – Gandalf Jun 26 '15 at 07:38
  • what is the use of [cell updateCellWithItem:self.items[indexPath.row]]; – Nischal Hada Jun 26 '15 at 09:24
0

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];

}
Jigar
  • 1,801
  • 1
  • 14
  • 29