0

So currently, I have a tableviewcell that looks like this

enter image description here

What I want to happen, is that if an expense of the date already exists, the top label should disappear, the tableviewcell height should be reduced from 95 to 64 and everything should be centrally aligned. Sort of like this

enter image description here

I tried doing this many ways.

  1. Use 2 different cells and switch, but that didn't work as only one expense was returned at a time and my tableviewcontroller didn't populate correctly.

  2. Try using a stack view, but in that, I can't get the constraints to match as they are currently.

I have all the correct row height being returned in the heightForRowAtIndexPath method, but it centrally reduces the height and some of the data is cut.

How is it possible to achieve what I want to do (have the label not visible, the row height reduced and everything vertically center)?

Here is the code for switching of the cells.

func tableView(_ tableView: UITableView,
               heightForRowAt indexPath: IndexPath) -> CGFloat
{
        if newMode==true {
            return 95
        }
        else if newMode==false {
            return 64
        }
        else {
            return 0
        }
}

This works, however it reduces the height from the top and the bottom and I only want the height from the top to be reduced.

A. G
  • 631
  • 2
  • 9
  • 19
  • Can you provide code regarding that? With stackview it should be done. – Payal Maniyar Dec 21 '17 at 05:11
  • With stackView, everything comes into one line and I am unable to apply constraints. – A. G Dec 21 '17 at 05:19
  • post your code @A.G – Ashish Bahl Dec 21 '17 at 05:23
  • Done, is there any other code you need? – A. G Dec 21 '17 at 05:29
  • @A.G You can try this code may be it will be helpful to you and its working fine like your conditions --- https://github.com/pradeepkas/ResizingCellWithImageAndText/blob/master/ResizeCellLblViewController.swift – Pradeep Kashyap Dec 21 '17 at 05:30
  • have you used vertical stackview? – Payal Maniyar Dec 21 '17 at 05:30
  • or provide storyboard file – Payal Maniyar Dec 21 '17 at 05:35
  • Date of Expanses is Tableview SectionHeader? – Kiran Sarvaiya Dec 21 '17 at 05:38
  • 1
    @A.G Get the outlet for height constraint of that label, and before your table view is populated , check if expense of date exist or not , if yes, then set height constraint = 0 and update cell height accordingly. I guess you can make use of enum here. – Tushar Sharma Dec 21 '17 at 05:39
  • I agree with Tushar's suggestion and if you do not want to write extra code for cell hieght then use Automatic Dimension cell. It helps table view to calculate each cell's height dynamically at run time, just make sure your constraint chaining is correct and everything else will be handled by Automatic Dimension by just setting that label hieght of date of expense as 0. For Automatic dimesnion you can refer : https://www.raywenderlich.com/129059/self-sizing-table-view-cells – Pallavi Srikhakollu Dec 21 '17 at 06:09
  • @TusharSharma if I set the height constraint to zero, wouldn't it just pin the label to the top of the cell. – A. G Dec 21 '17 at 06:50

1 Answers1

0

You should not use the delegate method heightForRowAt instead you should use:

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 80 

then give the (date of expense) label a hight constraint and connect an outlet to it.

in cellForRowAtIndex delegate method should have :

if expense != nil 
{
   lblExpenseConstraintHeight.constant = 0
}
else
{
   lblExpenseConstraintHeight.constant = 34
}
cell.layoutIfNeeded()

Edit:

More info about about dynamic tableViewHeight:

Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

Another possible solution is remove the height constraint of the (date of expense) label and set it's text to empty string.

Ayman Ibrahim
  • 1,359
  • 15
  • 24
  • This works, however doesn't reduce the height of the cell to 64. All it does is make the label and text invisible. – A. G Dec 21 '17 at 08:01
  • @A.G call tableView.reloadData() Or tableView.beginUpdates() ... tableView.endUpdates() – Ayman Ibrahim Dec 21 '17 at 08:17
  • @A.G You do not seem to know how table view dynamic cell height works, estimatedRowHeight 64 -> this value is estimated in order for the table view to know the scrolling height to adjust the scroll bar. it does not give the table cell fixed cell height. – Ayman Ibrahim Dec 21 '17 at 08:29
  • Ok, but then if I want the height of the row to be adjusted to 64, what can I do? – A. G Dec 21 '17 at 08:33
  • @A.G you have two alternatives for this, 1- if you want to use the dynamic table view cell height feature, give the parent view of the cell (put all the cell sub views inside one view) and give it fixed size height constraint equal to 64. 2- use the method as you were doing heightForRowAt delegate method. – Ayman Ibrahim Dec 21 '17 at 08:39