Question

I am almost new to create a custom cell. I have a custom cell and created 6 labels on each row with a custom button(btnEdit). The custom button is droped from .xib. The btnEdit create a frame on two label and if that frame is clicked it call another function which is working fine.

The only issue I have is if I click on one of the btnEdit in the row I don't want it to be clicked in the other row unless it is removed OR if one is selected and the other clicked it remove the first and frame the other.

Here is my code that I hope it helps;

.H

@interface PositionTableCell : UITableViewCell {
IBOutlet UILabel *lblSymbol;
IBOutlet UILabel *lblSpl;
IBOutlet UILabel *lblLpl;
IBOutlet UILabel *lblrate;
IBOutlet UILabel *lblAmount;
IBOutlet UILabel *lblO;
}

@property (nonatomic, assign) BOOL isSelected;
@property (nonatomic, assign) BOOL isRowSelected;

@end

.M

- (IBAction)btnEdit:(id)sender
{
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(value1)];
tapGestureRecognizer.numberOfTapsRequired = 1;
[lblLpl addGestureRecognizer:tapGestureRecognizer];

UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(value2)];
tapGestureRecognizer2.numberOfTapsRequired = 1;
[lblSpl addGestureRecognizer:tapGestureRecognizer2];

if (!isSelected){

    lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor blueColor].CGColor;
    lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 5.0;
    lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= YES;

    isRowSelected = YES;
    isSelected = YES;
}

else if (isSelected){

    lblLpl.layer.borderColor = lblSpl.layer.borderColor = [UIColor clearColor].CGColor;
    lblLpl.layer.borderWidth = lblSpl.layer.borderWidth = 0;
    lblLpl.userInteractionEnabled = lblSpl.userInteractionEnabled= NO;

    isRowSelected = NO;
    isSelected = NO;
}

[tapGestureRecognizer release];
[tapGestureRecognizer2 release];

NSLog(@"CLICKED");
}
Was it helpful?

Solution

It can be solved by using keeping a reference of your previously selected cell.

Declare a variable in your .m file, like:

static PositionTableCell *previousCell = nil;

And modify your method like:

- (IBAction)btnEdit:(id)sender
{
   if (previousCell != nil && previousCell != self)
   {
       for (id subLabel in [[previousCell contentView] subviews])
       {
            if ([subLabel isKindOfClass:[UILabel class]])
            {
                 UILabel *tempLabel               = (UILabel *)subLabel;
                 tempLabel.layer.borderColor      = [UIColor clearColor].CGColor;
                 tempLabel.layer.borderWidth      = 0;
                 tempLabel.userInteractionEnabled = NO;

                 isRowSelected = NO;
                 isSelected    = NO;
            }
       }
   }


   // Other codes here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top