casillas
casillas

Reputation: 16793

Hide/Show a Label in custom cell in TableView

I have the following cell in TableView.

Initially, I assign height 0 for Promotion Label and its value which is hardcoded $10.

Whenever user clicks on Promotion button and I am making height bigger to show Promotion and its value and assign height 30.

However, the only changes that I see, height of the cell is getting bigger and spaces between first and second label is getting bigger, but I cannot able to see promotion label.

here is my code:

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

  static NSString *cellLastIdentifier = @"lastcell";
  LastTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellLastIdentifier];
  if (cell == nil)
       cell = [[LastTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellLastIdentifier];
   cell.subTotalLbl.text = [NSString  stringWithFormat:@"$ %.02f", subtotal ];
   cell.salesLbl.text = [NSString  stringWithFormat:@"+$ %.02f", tax ];
   if(isPromotionApplied)
   {
     cell.promotionLabelHeight.constant = 30;
     cell.promotionValueLblHeight.constant = 30;
     cell.promotionValueLbl.text = @"120";
    }
   return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(isPromotionApplied)
        return 140.0;
    else
         return 100.0;
}

- (void)applyPromoCode : (double)percent
{
   isPromotionApplied = YES;
   NSIndexPath* rowToReload = [NSIndexPath indexPathForRow:[sharedData.orderItems count] inSection:0];
   NSArray* rowsToReload = [NSArray arrayWithObjects:rowToReload, nil];
   [self.checkOutTableView reloadRowsAtIndexPaths:rowsToReload withRowAnimation:UITableViewRowAnimationFade];
}

LastTableViewCell storyboard

enter image description here

Upvotes: 0

Views: 761

Answers (2)

harshal jadhav
harshal jadhav

Reputation: 5684

Check the constraints your sales tax label should have vertical bottom constraint to promotion label and promotion label should have vertical bottom constraint to custom cell.

Now create a IBOutlet of height constraint of the promotion label and instead of show hide set the height to 0 and 20

heres a short demo https://github.com/harshalrj25/stackoverFlowAnswers

Upvotes: 2

Jitendra Solanki
Jitendra Solanki

Reputation: 772

For this you should set the auto layout constraint between the labels and also set the height constraint for the promotionlbl.

Then take an outlet of the height constraint of the promotionlbl and initially set the constraint's constant to 0 and when you want to show the promotionlbl, then again set height constant as per your need.

Upvotes: 1

Related Questions