0

I have a button and a label which are sub views to UITableView.

Initially label value is 0.

what i need is, when i click button on particular cell i want to increment value in same cell label (as 1) and display that value in same label.

And again i clicked same cell button the label in that cell should be increment (as 2) and display that value in same cell in UITableView.

My code..

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *Lbl;
UIButton *btn;
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    Lbl =[[UILabel alloc]init];
    [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
    [Lbl setBackgroundColor:[UIColor clearColor]];
    [Lbl setTextAlignment:NSTextAlignmentLeft];
    [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
    Lbl.tag=indexPath.row;
    [cell.contentView addSubview:Lbl];

    btn =[UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"add" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
    [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
    [btn setFrame:CGRectMake(289, 2, 30, 71)];
    btn.tintColor=[UIColor lightGrayColor];
    btn.tag=indexPath.row;
    [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:btn];
} else {
    Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
    btn =(UIButton *)[cell.contentView viewWithTag:indexPath.row];
}

cell.textLabel.text=@"title";
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

return cell;

}

// button action method
-(void)increaseItemCount:(UIButton *)sender
{    
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *path = [tableView indexPathForCell:cell];
NSLog(@"row: %d",path.row);
UILabel *countLbl =(UILabel *)[cell.contentView viewWithTag:path.row];
showItemCount=[countLbl.text intValue] + 1;
NSLog(@"%d",showItemCount);
countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

}

I tried this, After clicking the value is showing in other cells and when i scroll the table view that value is showing in all cells.

Any suggestions or code

4 Answers4

0

That's because it looks like you're using a single instance variable to store the itemCount. You should use an array to know which cell has been clicked how many times. The code is however pretty messy, so you should write it again from scratch

Vik
  • 1,897
  • 12
  • 18
  • Yes, you are right..i`am using one instance variable to store click count. Can u give some code to solve –  Aug 08 '13 at 07:31
  • You can simply use a `NSMutableArray` instance variable initialized with `@0` for every index. then where you have `showItemCount = [...]` you can do `myInstanceVariable[indexPath.row] = @(myInstanceVariable[indexPath.row] + 1);` and where you have `countLbl.text = [...]` you can write `countLbl.text = [NSString stringWithFormat:@"%@",myInstanceVariable[indexPath.row]];` – Vik Aug 08 '13 at 07:58
  • This line giving error, where selectedCells is mutablearray. selectedCells[indexPath.row]=@(selectedCells[indexPath.row] + 1); –  Aug 08 '13 at 09:19
  • Yes, I blindly typed that. You should probably wrap `selectedCells[indexPath.row]` with `[... intValue]` so that it should look like `= @([selectedCells[indexPath.row] intValue] + 1);` – Vik Aug 08 '13 at 09:25
  • Now, my app crashed error in this line countLbl.text = [NSString stringWithFormat:@"%@",selectedCells[indexPath.row]]; i`am using this line in cellForRowAtIndexPath method –  Aug 08 '13 at 09:39
  • [__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' –  Aug 08 '13 at 09:44
  • In my comment I said that you had to initialize the array with @0 for every index. That means you should do something like `[selectedCells addObject:@0]` in a `for` cycle for how many cells you have. Or you could also do in `cellForRowAtIndexPath` a check with `if([selectedCells count] <= indexPath.row) [selectedCells addObject:@0]` before and instead of incrementing the content. – Vik Aug 08 '13 at 09:53
0

please do as per following:

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    selectedIndex=-1;
    //[tblView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   // UILabel *Lbl;
   // UIButton *btn;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        UILabel *Lbl =[[UILabel alloc]init];
        [Lbl setFrame:CGRectMake(56, 60, 117, 12)];
        [Lbl setBackgroundColor:[UIColor clearColor]];
        [Lbl setTextAlignment:NSTextAlignmentLeft];
        [Lbl setFont:[UIFont boldSystemFontOfSize:15.0]];
        Lbl.tag=indexPath.row;
        Lbl.text=[NSString stringWithFormat:@"%i",0];
        [cell.contentView addSubview:Lbl];

        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"add" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.7 green:0 blue:0 alpha:1.0] forState:UIControlStateHighlighted];
        [[btn titleLabel] setFont:[UIFont boldSystemFontOfSize:23]];
        [btn setFrame:CGRectMake(289, 2, 30, 71)];
        btn.tintColor=[UIColor lightGrayColor];
        btn.tag=indexPath.row;
        [btn addTarget:self action:@selector(increaseItemCount:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:btn];

 NSLog(@"Selected Index:%i",indexPath.row);   
    } else {

       UILabel *Lbl =(UILabel *)[cell.contentView viewWithTag:indexPath.row];
      //UIButton *btn =(UIButton *)[cell viewWithTag:indexPath.row];

        if(indexPath.row==selectedIndex)
        {
             NSLog(@"Selected Index in CellForRowAtIndexPath:%i",indexPath.row);
            Lbl.text=[NSString stringWithFormat:@"%i",[Lbl.text intValue]+1];
        }

    }

   // cell.textLabel.text=@"title";
    //countLbl.text = [[NSString alloc] initWithFormat:@"%d",showItemCount];

    return cell;
}

-(void)increaseItemCount:(UIButton *)sender
{
    selectedIndex=sender.tag;
    [tblView reloadData];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Selected Index:%i",indexPath.row);
}

i hope this will help you.

hpp
  • 619
  • 3
  • 13
  • I want to increase count on button click –  Aug 08 '13 at 09:43
  • I tried,but count is increasing automatically when i scroll table view and count is increasing in random cells –  Aug 08 '13 at 10:22
0

indexPath.row begins 0 to n, we not set tag 0 for label and you use single variable showItemCount used to assign text in countLbl, table view reuse cell when it will appear,use array to store showItemCount for each cells

NANNAV
  • 4,875
  • 4
  • 32
  • 50
0

If you want source code, Download it from here https://github.com/MasudShuvo/TestCustomTableViewCell

I've modify the code as your requirement.

shuvo
  • 705
  • 5
  • 15
  • I tried your code it`s working. When i increase cell height to 65 the app is creased. It showing error like this "Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 7 beyond bounds [0 .. 6]' " –  Aug 08 '13 at 10:45
  • Did you change cell height on my project or yours? – shuvo Aug 08 '13 at 11:21
  • can u tell me how to stop decrementing value...when the value reaches 0 –  Aug 08 '13 at 12:02
  • can u tell me how to implement long press along with click –  Aug 08 '13 at 12:47
  • http://stackoverflow.com/questions/6179347/uibutton-long-press-event, http://stackoverflow.com/questions/11200079/long-pressed-uibutton please check this link, May be these will be helpful for you. – shuvo Aug 08 '13 at 13:00
  • When i run your project its working fine. when implement that into my project the app crashed. It showing arror like this -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array' –  Aug 08 '13 at 13:39
  • Nice to hear that :-). WC – shuvo Aug 08 '13 at 14:04
  • Hi, Shuvo. small help.......How to implement searchBar and searchDisplayController in your project. –  Aug 09 '13 at 12:23