0

I have a UITableView with CustomCell. Whenever UITableView is in editing mode i have following code in CustomCell.

- (void)willTransitionToState:(UITableViewCellStateMask)state{

    [super willTransitionToState:state];

    if (state == UITableViewCellStateShowingEditControlMask)
    {
        self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.delBtn setFrame:CGRectMake(10, 15, 25, 25)];
        [self.delBtn setImage:[UIImage imageNamed:@"noSelection.png"] forState:UIControlStateNormal];
        buttonCurrentStatus = YES;
        [self.delBtn addTarget:self action:@selector(delBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.delBtn];
    }
    else
    {
        if(self.delBtn)
        {
            [self.delBtn removeFromSuperview];
            self.delBtn = nil;
        }
    } }

- (void)delBtnPressed:(id)sender {
    if (buttonCurrentStatus == NO)
    {
        buttonCurrentStatus = YES;
        [self.delBtn setImage:[UIImage imageNamed:@"noSelection.png"] forState:UIControlStateNormal];
    }
    else
    {
        buttonCurrentStatus = NO;
        [self.delBtn setImage:[UIImage imageNamed:@"selection.png"] forState:UIControlStateNormal];
    } }

Now how can i get indexPath from CustomCell of UITableview ?

DipakSonara
  • 2,598
  • 3
  • 29
  • 34

2 Answers2

0

You can use UITableView indexPathForCell:.

Obviously, for that you'll need a reference to the table inside the cell class. If you want to make it a property of your cell, be sure to make it weak to avoid a reference cycle.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
0

I have found the best way of doing this is to create Delegate/Protocol on cell. Make the delegate the ViewController and pass it in. Then you can call indexPathForCell on the tableview at that point. Example below.

- (void)willTransitionToState:(UITableViewCellStateMask)state{

    [super willTransitionToState:state];

    if (state == UITableViewCellStateShowingEditControlMask)
    {
        self.delBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.delBtn setFrame:CGRectMake(10, 15, 25, 25)];
        [self.delBtn setImage:[UIImage imageNamed:@"noSelection.png"] forState:UIControlStateNormal];
        buttonCurrentStatus = YES;
        [self.delBtn addTarget:self action:@selector(delBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.delBtn];
    }
    else
    {
        if(self.delBtn)
        {
            [self.delBtn removeFromSuperview];
            self.delBtn = nil;
        }
    } }

- (void)delBtnPressed:(id)sender {
    if (buttonCurrentStatus == NO)
    {
        buttonCurrentStatus = YES;
        [self.delBtn setImage:[UIImage imageNamed:@"noSelection.png"] forState:UIControlStateNormal];
    }
    else
    {
        buttonCurrentStatus = NO;
        [self.delBtn setImage:[UIImage imageNamed:@"selection.png"] forState:UIControlStateNormal];
    }

    if ([self.delegate respondsToSelector:(customCelldelBtnPressed:)]) {
        [self.delegate customCelldelBtnPressed:self];
    }
}

// of course you will need to actually create the delegate or protocol and implement it on the view controller.

NANNAV
  • 4,875
  • 4
  • 32
  • 50