Dynamically adjust cell height under iOS development--autolayout

Source: Internet
Author: User

Scenario Description:

Sometimes we want to update the data for one cell, and it's common practice to update the entire cell with the Reloaddata method. However, it is not applicable to some situations or it is more troublesome to realize it. For example, this simple "point-and-click" cell

The hero's profile is replaced with "Click to view Details" and the download button is set to hidden when it is not opened.

In this case, if the data is reload the entire table at the point of opening, the table will revert to an open state because of the overloaded data.

Steps to resolve:

The advantage of using AutoLayout is that it allows the content to determine the height of the cell. So what is the way to adjust the cell height dynamically after the content has changed? Let's start with the constraint and write the constraint of the cell so that it adapts well to the state before and after the content changes. (see this article for TableView restrictions: http://www.cnblogs.com/zeyang/p/4457888.html)

But you'll find that no matter how you change it, the height of the cell will always be the same when the cell point is opened. Like this:

The reason is that we only tell the contents of the system cells to be changed, and the system thinks that the height of the cell is still the original height, so the content is compressed instead of the height of the cell. But Reloaddata will reload the entire table and not meet the requirements.

We have the following methods:

   1:  -(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) indexpath{
   2:      //The most magical place!! 
   3:      [TableView beginupdates];
   4:      [TableView endupdates];
   5:  }
   6:  -(void) TableView: (UITableView *) TableView Diddeselectrowatindexpath: ( Nsindexpath *) indexpath{
   7:      //The most magical place!! 
   8:      [TableView beginupdates];
   9:      [TableView endupdates];
  Ten:  }

With these two words, the system recalculates the height of the cell each time the cell is selected, and when using AutoLayout, the cell can display the contents of the cell very well.

Reference Source:

[IOS] Change cell height with animation effect in TableView

Dynamically adjust cell height under iOS development--autolayout

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.