Key Point of UITableViewCell highly adaptive, ioscell highly adaptive

Source: Internet
Author: User

Key Point of UITableViewCell highly adaptive, ioscell highly adaptive

There are already many articles about UITableViewCell highly adaptive in iOS development. However, if the cell content is complicated, you still cannot use autolayout to configure auto-use at one time.

KEY POINT

Here we will only talk about the key points of setting:

CellInternalConstraintsThere must be a slaveCellA connectable line from top to bottom.

Legend list:


Paste_Image.png
Paste_Image.png
Paste_Image.png

The top and bottom elements are separated from the bottom and added with constraints.
This lineThere can be fixed height elements and adaptive height elements, but ensure that:

All the elements that set the adaptive height are on this line.

Of course, if there are two elements that require adaptive height at the same level, you can choose one.

OTHER POINTS

Other considerations:


Paste_Image.pngLAST

Finally, add the code in the code file:
IOS8
How to Use iOS8:

self.tableView.estimatedRowHeight=44;self.tableView.rowHeight=UITableViewAutomaticDimension;

IOS7
(Let's discard iOS7 in history .)
Without UITableViewAutomaticDimension in ios, you can only calculate the height by yourself.
Ios7 requires an auxiliary cell. The cell content is consistent with that of UITableView, but is not used for display. Instead, it is used to calculate the height of each cell based on the filled content.

@property (strong,nonatomic) AdaptionCell *adaptionCell;
//-(Void) viewDidLoad why cannot I set a cell using [tableView dequeueReusableCellWithIdentifier: cellIdentifier forIndexPath: indexPath. This causes the cell to be created but not returned by the tableView: cellForRowAtIndexPath: method, which may cause memory leakage. Self. adaptionCell = [cellNib instantiateWithOwner: nil options: nil] [0];
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ self.adaptionCell.contentLabel.text=self.dataArray[indexPath.row%2];    [self.adaptionCell layoutIfNeeded];    CGFloat height = [self.adaptionCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;    return height+1;}

Why do we need to add more than 1, because UITableViewCell has an edge line, which is 1 higher than its ContentView (in fact, it should be 0.5)

The systemLayoutSizeFittingSize function used in the above method gets the size of contentView and finally obtains an adaptive height.

But there is a problem here-why does this method get an adaptive height instead of an adaptive Width!
Therefore, there should be a place to limit the Width so that the content of the Cell is filled vertically rather than horizontally.

//  AdaptionCell.m- (void)layoutSubviews{    [super layoutSubviews];    [self.contentView layoutIfNeeded];    self.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.contentLabel.frame);}
LAST

The final result is shown in the following figure:


Paste_Image.png

And a question:
Can autolayout be used for self-adaptation if the cell requires highly adaptive text to be rich text?
SimpleUILabel attributedTextYou can use



Author: Seas
Link: http://www.jianshu.com/p/386b792054b1
Source: Simplified book
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.