the effect to be achieved Requirements:
General Titlelabel displays only one row of headings, with a fixed height.
The ImageView size is also fixed.
The Detaillabel width is fixed, but the height is dynamically adjusted according to the text.
The bottom of the cell bottom imageview and the bottom height of the Detaillabel are greater than or equal to 20.
When the Detaillabel text is very small, the bottom of the cell refuses ImageView to maintain 20, when the bottom of Detaillabel is greater than 20.
When Detaillabel text is many, the cell bottom is more than 20 from the bottom of ImageView, and the bottom height of the Detaillabel maintains 20.
Storyboard on the prep work
Note Set Detaillabel numberoflines to 0
Build the cell custom Autocell class, correlate the controls, and prepare some data sources beforehand.
NameArray = [Nsmutablearray arraywithobjects:@ "Volute", @ "AI", @ "Big Janroe", nil]; Imagearray = [Nsmutablearray arraywithobjects:@ "u=4040080498,3072784853&fm=90&gp=0.jpg", @ "u= 2384677404,2895132414&fm=21&gp=0.jpg ", @" u=262781505,2408318453&fm=21&gp=0.jpg ", nil]; Descriptionarray = [Nsmutablearray arraywithobjects:@ "volute play good chic, good elegant, iron king of the King," @ "AI, the history of the shortest champion, uncrowned king, crossover is very powerful, ornamental strong, FOREVER MVP!!!! "@" The youngest of 10,000 Mr., MVP, O ' Brien Cup, King of efficiency, favored, the strongest player in the universe without one, strong burst buckle body stick, hairline and high, the key moment again shrugged, lead the Knight Championship, although it seems to be not!!!!!! ", nil];
Implementation of the TableView method of delegation
numberofrows-(Nsinteger) TableView: (uitableview *) Tableview numberofrowsinsection: (NSInteger) Section{ return 3;} cellforrow-(uitableviewcell*) TableView: (uitableview *) Tableview cellforrowatindexpath: (NSIndexPath *) indexpath{ autotableviewcell *cell = ( autotableviewcell*) [tableview dequeuereusablecellwithidentifier:@ "Autocell"]; [ cell.titlelabel settext:nil]; [cell.titlelabel settext:[namearray objectatindex:indexpath.row]]; [cell.descriptionlabel settext:nil]; [cell.logoimageview setimage:[uiimage imagenamed:[imagearray objectatindex: indexpath.row]]]; [cell.descriptionlabel settext:[descriptionarray Objectatindex:indexpath.row]]; return cell;}
Do not implement the Heightforrow method, run directly, found that the IOS7,IOS8 did not get the desired effect
IT ' S so bad!!!
Core part, Heightforrow method implementation
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{static Autotableviewcell *cell = nil; Static dispatch_once_t Oncetoken; Will only walk once Dispatch_once (&oncetoken, ^{cell = (autotableviewcell*) [TableView dequeuereusablecellwithidentifier : @ "Autocell"]; }); Calculate cgfloat height = [cell Calulateheightwithttitle:[namearray ObjectAtIndex:indexPath.row] desrip:[ Descriptionarray ObjectAtIndex:indexPath.row]]; return height;}
The specific calculation height method is now in the custom cell class
-(CGFloat) Calulateheightwithttitle: (nsstring*) title Desrip: (nsstring*) descrip{//Here very important cgfloat Premaxwaith =[UISCR Een mainscreen].bounds.size.width-108; [Self.detaillabel Setpreferredmaxlayoutwidth:premaxwaith]; [Self.titlelabel Settext:title]; This is also very important [Self.detaillabel layoutifneeded]; [Self.detaillabel Settext:descrip]; [Self.contentview layoutifneeded]; Cgsize size = [Self.contentview systemlayoutsizefittingsize:uilayoutfittingcompressedsize]; Plus 1 is the key return size.height+1.0f;}
A few important places to watch out for
-
First of all, why do you want to set Preferredmaxlayoutwidth, which represents the maximum layout width of the label, the label shows how many lines are definitely related to its width, all here to set the correct width, but here's a bit of a pit place
This is storyboard on the Detaillabel of this property, the default is not checked (automatic) means the system automatically calculates the maximum layout width, but to view the official documents, you will find that the automatic calculation only in the iOS8 will have effect, less than iOS8 will not be automatically calculated. Then you might say, "Hook it up!!!"
, what do you mean when you find that the 492 is displayed after the tick? This number is the width of the currently used storyboard minus the absolute distance from the label to the two boundaries. The Xcode6 is a large size storyboard width of 600, minus the Detaillabel to the left boundary 98, minus 10 from the right edge, just 492.
But is that right? Obviously not, theiphone screen width is not already 3 kinds of width? 320, 375 (iphone6), 414 (plus)
So 600 is obviously wrong, should use the current running width to subtract 108, so here tick is wrong, simply do not tick the direct code to calculate it ....
CGFloat Premaxwaith =[uiscreen mainscreen].bounds.size.width-108;
-
About layoutifneeded in the end is what, I also smattering, only know not add effect out, plan to consult later ...
-
Plus 1 is the key.
[Self.contentview Systemlayoutsizefittingsize:uilayoutfittingcompressedsize];return size.height+1.0f;
Here size.height is actually the height of the contentview we want, but if we return this height directly, we give the cell the height, but because of the cell boundary, thecell height is 1 more than the contentview height, So add 1 here and return. Do not underestimate the 1 pixels, less its effect is really not come out!!!!
Notice this, we run again, find the desired effect, switch the simulator, no problem.
In short, the study of the layout of a few days, found iOS good pit, a variety of traps, fortunately consulted a variety of domestic and foreign information, and ultimately achieve the effect.
This set of code has not been on the IOS6, IOS6 on the situation is not the same, until I go to study .....
IOS 7.0+ Autolayout-tableviewcell height