UITableView inside the cell fixed height that is basically impossible, a lot of functions and interface will involve highly adaptive, and e-commerce class is particularly common, before all is their own calculation, the code is written very much, the logic has not written, light this layout UI and height calculation can be close to 1000, When you're done, the key points out the bug.
That was the case:
Suddenly see Uitableview+fdtemplatelayoutcell this library on GitHub
Portal: Click to open link
It was like this when I first saw it:
Can you use it??? Is it really so???
This is the first time you use it:
Dude, are you kidding me, what the hell, never seen such a scene, then I was confused ...
After a few times, the effect is as follows:
Ah yo, good yo ... It feels a little bit.
OK, look at the basic process.
Since we still use IB to layout, Operation Nsconstraintlayout class to carry out the calculation, so the official web-based layout of the case posted, as follows
Note:
a fully self-satisfied cell is constrainted by auto layout and each edge (" Top "," left "," bottom "," right ") have at least One layout constraint against it
An adaptive height cell is constrained by AutoLayout, and the constraints of all internal controls are integrated, at least one time from the cell four edge ( top left bottomright), or bad one
As shown in the following:
The direction that the arrow refers to must appear at least once is a complete set of constraints
1. First create a new project, build a tableview out, set up a good agent, layout of the basic framework
2. Then get a cell, layout play after the key arrow refers to the two constraints dragged out, later to use, first look at the main layout, the demo finally gave
3. I find a few data sources, I personally encountered is, if each cell has a picture, or no picture, and the height of the picture, then the server also requires the return of the specific image width and height, for us to directly carry out the proportional calculation, and then in the following proxy method layout time to operate
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{ Mkjtableviewcell *cell = [TableView dequeuereusablecellwithidentifier:identify forindexpath:indexpath]; [Self Configcell:cell indexpath:indexpath]; !< This method will also use the return cell;}
The most critical approach
-(void) Configcell: (Mkjtableviewcell *) cell Indexpath: (Nsindexpath *) index{//Get model Mkjmodel *model = SELF.DATASOURC E[index.row]; __weak typeof (cell) Weakcell = cell; To configure a picture of a user picture, the following is an animated animation that lets picture 0-1 fade [Cell.headimageview sd_setimagewithurl:[nsurl URLWithString:model.headImageURL] completed:^ (UIImage *image, Nserror *error, Sdimagecachetype cachetype, Nsurl *imageurl) {if (Image && CAC Hetype = = Sdimagecachetypenone) {weakCell.headImageView.alpha = 0; [UIView animatewithduration:1.0 animations:^{weakCell.headImageView.alpha = 1.0; }]; } else {WeakCell.headImageView.alpha = 1.0; } }]; User name cell.userName.text = model.username; User description Cell.descLabel.text = model.desc; When the picture is present if (![ Model.mainimageurl isequaltostring:@ ""]) {//Picture not hidden Cell.mainImageView.hidden = no; Configure the main diagram method, the same avatar [Cell.mainimageview Sd_setimaGewithurl:[nsurl URLWithString:model.mainImageURL] completed:^ (UIImage *image, Nserror *error, Sdimagecachetype CacheType, Nsurl *imageurl) {if (image && CacheType = = Sdimagecachetypenone) { WeakCell.mainImageView.alpha = 0; [UIView animatewithduration:1.0 animations:^{weakCell.mainImageView.alpha = 1.0; }]; } else {WeakCell.mainImageView.alpha = 1.0; } }]; We calculate the constraint cell.mainHeightConstraint.constant = (model.mainheight * 375)/model.mainwidth for the height of the main graph just now. At the bottom of the description of the specific image of the constraint fixed to cell.descTopConstraint.constant = 19; }//Picture no time, only show description {//main picture hidden Cell.mainImageView.hidden = YES; The higher the height of the main figure is 0 actually also represents the hidden cell.mainHeightConstraint.constant = 0; At the bottom if the constraint is still 19, it will pull too long, so let's make him 0 cell.descTopConstraint.constant = 0; }}
4. The time has come to witness miracles, as the official saying goes:
#import <uitableview+fdtemplatelayoutcell.h>-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath { return [TableView fd_heightforcellwithidentifier:@] Identifer "Cachebyindexpath:indexpath configuration:^ (id cell) { //config cell data source, and" Cellforrow "do the same thing, for example: [self Configcell: (UITableViewCell *) cell Indexpath: (Nsindexpath *) Indexpath]; }];}
People
have said, a word to fix, anyway, I was like this:
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{ return [ TableView fd_heightforcellwithidentifier:identify cachebyindexpath:indexpath configuration:^ (MKJTableViewCell * Cell) { [self Configcell:cell Indexpath:indexpath];} ];}
I think it is very useful, or thanks to the author of the Ox B!
Line thrives!!! The author's life safe!!!
Let's be relaxed and happy, no more tangled up in the frame, the simple effect is as follows
The demo's GitHub address: Demo address, required points I have any comments you tell me, humbly ask
over~~~~~~~~~~~~~~~~~~
IOS Uitableview+fdtemplatelayoutcell with AutoLayout minutes teaches you to achieve dynamic, highly adaptive