IOS extends UITableViewCell on click with AutoLayout

I am working on a simple application, and now I am focused on extending the UITableViewCell after the user has touched this cell. This is an iOS 8 application, so I installed:

self.tableVIew.rowHeight = UITableViewAutomaticDimension self.tableVIew.estimatedRowHeight = 50 

cell restrictions are as follows:

enter image description here

If the custom responder cell calls this function:

 func extend() { self.contentView.removeConstraint(self.bottomConstraint) let additionalView = UIView() additionalView.setTranslatesAutoresizingMaskIntoConstraints(false) additionalView.backgroundColor = UIColor.orangeColor() self.contentView.addSubview(additionalView) self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[additionalView(50)]-5-|", options: nil, metrics: nil, views: ["additionalView" : additionalView])) self.contentView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-5-[additionalView(100)] -5@999- |", options: nil, metrics: nil, views: ["additionalView" : additionalView])) } 

self.bottomConstraint is the price between the bottom of the green circle and the contents of the content below.

Question: Why does this solution only work if the restriction has priority:

 V:|-5-[additionalView(100)] -5@999- | 

?

Without an explicit priority, I got errors:

 ( "<NSLayoutConstraint:0x7a63f7a0 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7a737840(60)]>", "<NSLayoutConstraint:0x7a644f40 V:|-(5)-[UIView:0x7a63f2a0] (Names: '|':UITableViewCellContentView:0x7a737840 )>", "<NSLayoutConstraint:0x7a644fb0 V:[UIView:0x7a63f2a0(100)]>", "<NSLayoutConstraint:0x7a644f70 V:[UIView:0x7a63f2a0]-(5)-| (Names: '|':UITableViewCellContentView:0x7a737840 )>" ) Will attempt to recover by breaking constraint <NSLayoutConstraint:0x7a644f70 V:[UIView:0x7a63f2a0]-(5)-| (Names: '|':UITableViewCellContentView:0x7a737840 )> 
+5
source share
3 answers

I did a demo that gives you the exact behavior you want. Link here: https://github.com/rushisangani/TableViewCellExpand

Please set a limit for expanding / collapsing views, as described in the demo.

+2
source

'UIView-Encapsulated-Layout-Height' is the limit for the height of the row created after the table view system has calculated what it should be. Simply changing the constraint inside is not enough to force a recount.

This is not a new unexpected behavior; while effects can sometimes be achieved by allowing cells to depend on their height given by the table, the delegate of the data source of the presentation table is designed to provide an “official” row height.

Apple sample "Animation and gestures in the form of a table . " It has not been updated for iOS 8 with self-tuning cells, but it is worth looking at regardless. In APLTableViewController.m, tableViewController is a delegate for cell pitch gestures because it needs to adjust the height. Its handlePinch: calls updateForPinchScale: atIndexPath: which does the extension as you want.

The table controller is not trying to directly change the row height. First, it sets its own custom data source object (called sectionInfo) to provide an updated row height value [sectionInfo replaceObjectInRowHeightsAtIndex:indexPath.row withObject:@(newHeight)]; , Then the table view controller performs the following actions:

 BOOL animationsEnabled = [UIView areAnimationsEnabled]; [UIView setAnimationsEnabled:NO]; [self.tableView beginUpdates]; [self.tableView endUpdates]; [UIView setAnimationsEnabled:animationsEnabled]; 

Disabling all UIView animations is due to the fact that this is a pinch, so it should change instantly without sharpness. Since you only need a one-time change, you should experiment with different animation settings to get the desired effect.

They are a couple of calls to beginUpdates / endUpdates , which apparently make the system recount the line height. It is possible that with the new cells for iOS self calibration, simply changing the restrictions and calling these methods may be enough; you can also try reloadRowsAtIndexPaths:withRowAnimation:UITableViewRowAnimationNone sandwich

0
source

set the height priority of one of your views to 999 instead of 1000.

-2
source

Source: https://habr.com/ru/post/1213473/


All Articles