2

In my app, I'm updating the footer text of my table view dynamically when I select a cell. Unfortunately the way I'm doing so seems to alter the frame of the UILabel within my UIView. For example, when I tap a cell, the width of my label becomes something between 950 and 1200. When in reality the width should be (at most) the width of the view. Here's my code:

def tableView(tableView, viewForFooterInSection:section)
  (section != (self.sections.count - 1)) ? footer_view : nil
end

def tableView(tableView, heightForFooterInSection: section)
  if section != (self.sections.count - 1)
    footer_label.frame.size.height + 20.0
  else
    1.0
  end
end

def tableView(tableView, didSelectRowAtIndexPath: indexPath)
  tableView.beginUpdates
  footer_label.text = "Details: #{updated_details}"
  footer_label.sizeToFit
  tableView.endUpdates
end


def footer_view
  @footer_view ||= UIView.alloc.initWithFrame(CGRectZero).tap do |fv|
    fv.addSubview(footer_label)
  end
end

def footer_label
  @footer_label ||= UILabel.alloc.initWithFrame(CGRectZero).tap do |pl|
    pl.frame = CGRect.new(
      [15, 10],
      [self.view.frame.size.width - 30, self.view.frame.size.height]
    )
    pl.font = UIFont.preferredFontForTextStyle(UIFontTextStyleCaption2)
    pl.numberOfLines = 0
    pl.lineBreakMode = NSLineBreakByWordWrapping
    pl.text          = ""
    pl.sizeToFit
  end
end

I'm guessing calling sizeToFit is part of the problem?

Frederik Winkelsdorf
  • 4,383
  • 1
  • 34
  • 42
tvalent2
  • 4,959
  • 10
  • 45
  • 87
  • Is your case similar to [this one](http://stackoverflow.com/questions/39825290/make-uicollectionview-header-dynamic-height/39832379#39832379)? – Ahmad F Oct 16 '16 at 14:23

2 Answers2

2

I thought I had tried this but maybe my order was off. Either way, I solved it by setting the footer's frame again inside of beginUpdates/endUpdates:

tableView.beginUpdates
frame = footer_label.frame
footer_label.text = "Plan Details: #{updated_details}"
frame.size.width = self.view.frame.size.width - 30
footer_label.frame = frame
footer_label.sizeToFit
tableView.endUpdates
tvalent2
  • 4,959
  • 10
  • 45
  • 87
1

I think you'll need to use a dynamic height for the footer.

tableView.heightForFooterInSection = UITableViewAutomaticDimension
tableView.estimatedSectionFooterHeight = 25.0

Remember that you have to set the constraints of the UILabel. Maybe you'll have to update the section to change the height.

Zeb
  • 1,715
  • 22
  • 34