2

I have a custom cell that have a check box,

all is working fine, the checkboxes get check according to a dictionary that I pass to my subclassed UITableViewCell,

but now I need to pass to the class that have the table view the exact cell that my check box was modified so I can set my mutable dictionary with the new checked or unchecked state for that particular cell,

So how to do this?, shall I use a delegate?, this is fine, but the question is, how do I know what cell was my check box modified at?

manuelBetancurt
  • 15,428
  • 33
  • 118
  • 216
  • This should help. http://stackoverflow.com/questions/1750753/uitableviewcell-accessory-type-checked-on-tap-set-other-unchecked – Mark McCorkle May 13 '13 at 10:44
  • Hi @MarkM, I thougth of this, but my didSelectRow, doesnt get called, Im showing another view controller on didSelectRow, I even do a log but this doesnt get called on check of the cell, – manuelBetancurt May 13 '13 at 10:48
  • Yeah, didSelect only gets called when nothing else consumes the touch on the cell. If the cell contains a button, textField, switch, slider, etc... then that control will consume the touch (if the touch is inside it) and stop didSelect from firing. – Fogmeister May 13 '13 at 13:18

5 Answers5

4

You can use a delegate like this...

MyCell.h

@protocol MyCellDelegate <NSObject>

-(void)cellCheckBoxWasChanged:(MyCell *)cell;

@end

@interface MyCell : NSObject

@property (nonatomic, weak) id <MyCellDelegate> delegate;

@end

MyCell.m

@implementation MyCell

- (void)checkBoxChanged
{
    [self.delegate cellCheckBoxWasChanged:self];
}

@end

Then to find the index you can do...

TableViewController.m

- (void)cellCheckBoxWasChanged:(MyCell *)cell
{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];

    // do something to your array.
}
Fogmeister
  • 76,236
  • 42
  • 207
  • 306
1

Why don't you pass the UITableViewCell also in the delegate method as self.

So with that cell, you could get the indexpath by

NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
1

set the tag Value of each Checkboxes depending on the cells Indexpath in the cellForRowAtIndexPath: method.

   UIButton *checkboxes = customCell.checkButton
   [checkboxes setTag:indexPath.row];

then in buttons action method.

check the senders.Tag value to get the exact row of the button pressed

UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:sender.tag inSection:0]];
Bonnie
  • 4,943
  • 30
  • 34
1

you can do this -

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

    if (cell == nil) 
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

 if([[[Usercontacts objectAtIndex:indexPath.row]objectForKey:@"isChecked"]isEqualToString:@"NO"])
        {
            [checkUncheckBtn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];
             checkUncheckBtn.tag=1; 
        }
        else
        {
            [checkUncheckBtn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];
              checkUncheckBtn.tag=2; 
        }
       [checkUncheckBtn addTarget:self action:@selector(checkUncheckBtnPressed:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

and when you perform checkUncheckBtnPressed: method it looks like

-(void)checkUncheckBtnPressed:(id)sender
{

    UIButton *btn=(UIButton*)sender;
    UITableViewCell *cell =(UITableViewCell *) [sender superview] ;
    NSIndexPath *_indxpath = [createGroupContactsTableView indexPathForCell:cell];

    if(btn.tag==1)
    {
        [btn setImage:[UIImage imageNamed:@"checked box.png" ]forState:UIControlStateNormal];   
        btn.tag=2;
        [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"YES" forKey:@"isChecked"];
    }
    else 
    {

        [btn setImage:[UIImage imageNamed:@"unchecked box.png" ]forState:UIControlStateNormal];   
         btn.tag=1;
        [[Usercontacts objectAtIndex:_indxpath.row]setObject:@"NO" forKey:@"isChecked"];

    }


}
iEinstein
  • 2,100
  • 1
  • 21
  • 32
  • `UITableViewCell *cell =(UITableViewCell *) [sender superview] ;` isn't reliable. The checkbox is more likely to be a subview of the tablesView's contentView and possibly even nested deeper. – Mike Pollard May 13 '13 at 11:13
  • yeah it is possible but according to my project i have created according to my need.If you wish you can nest more – iEinstein May 13 '13 at 11:16
1

Here's an alternative to having the Cells listen to events from the checkBoxes and forward them to the UITableViewController using the delegate pattern:

Have the UITableViewController listen to events from the checkBoxes and use the following code to determine the NSIndexPath of the cell:

@implementation UITableView (MyCategory)

-(NSIndexPath*)indexPathOfCellComponent:(UIView*)component {
    if([component isDescendantOfView:self] && component != self) {
        CGPoint point = [component.superview convertPoint:component.center toView:self];
        return [self indexPathForRowAtPoint:point];
    }
    else {
        return nil;
    }
}
@end
Mike Pollard
  • 10,195
  • 2
  • 37
  • 46