3

In my code I have a table view with many sections sorted alphabetically. I have checkboxes(UIButton) in table rows and I need check them according to respective status entered in row. I had done

checkbutton.tag = indexpath.row; 

but when table scrolls and section get changed and I get status of previous section in my checkbox method. anyone please help me on this.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   CheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
   [CheckButton addTarget:self action:@selector(CheckBoxAction:)      forControlEvents:UIControlEventTouchUpInside];
   CheckButton.tag=indexPath.row;
   //some other stuff
   [cell.contentView addSubview:CheckButton];
}

and this my CheckBoxAction

-(void)CheckBoxAction:(UIButton *)btn
{
    tag = btn.tag;
    NSDictionary *tagdict =[statusArray objectAtIndex:tag]; 
}

Now, problem is that indexpath.row is 0 whenever section is changed, so I can't able to access exact row number in my CheckBoxAction.

Girish
  • 4,692
  • 4
  • 35
  • 55
Vikas
  • 914
  • 7
  • 19

3 Answers3

2

use this for getting current row you tap.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 CheckButton = [[UIButton alloc] init];

[CheckButton addTarget:self action:@selector(CheckBoxAction:)     forControlEvents:UIControlEventTouchUpInside];
CheckButton.tag=indexPath.row;
[cell addSubview:CheckButton];
}


-(void)CheckBoxAction:(id)sender
{
  // Cast Sender to UIButton
UIButton *button = (UIButton *)sender;

// Find Point in Superview
CGPoint pointInSuperview = [button.superview convertPoint:button.center toView:self.tableView];

// Infer Index Path
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInSuperview];

// Log to Console
NSLog(@"selected Row : %d", indexPath.row);
}
ChandreshKanetiya
  • 2,414
  • 5
  • 27
  • 41
1

// Try this

CheckButton.tag = [[NSString stringWithFormat:@"%d%d",indexPath.section,indexPath.row] integerValue];

or

// try this below link to get logic for, how to handle checkbox in UITableView

Set checkmark in UITableView

Community
  • 1
  • 1
Lokesh Chowdary
  • 816
  • 5
  • 22
0

What you are doing is wrong in many ways. So, I am not going to give you a solution for the exact problem you have, but more of a guideline:

  1. First you are adding the checkbox directly to the cell, which means you are possible adding multiple CheckButton to the same cell, if you scroll your UITableView a few times.

  2. Add the checkButton on the cell directly instead of keep adding it in the cellForRowAtIndexPath, this can give you a performance boost.

  3. Use a block that will be executed when a user clicks the button. This way you are able to capture the context of your UIViewController. You should assign that block to the cell on cellForRowAtIndexPath

Rui Peres
  • 25,741
  • 9
  • 87
  • 137
  • can you please explain how to Add the checkButton on the cell directly instead of keep adding it in the cellForRowAtIndexPath – Vikas Jul 19 '13 at 10:28
  • You can either add in `awakeFromNib` inside your Cell. Or, if you don't have a NIB, you can add it init method of your cell. But again, this is not a concrete answer to your question, just more a guideline. – Rui Peres Jul 19 '13 at 10:32
  • sir i can't understand.can you provide some link,tutorial or any type of help to add the checkButton on the cell directly – Vikas Jul 19 '13 at 10:37