Dynamically changing cell heights and IOS8 dynamic type understanding under IOS8

Source: Internet
Author: User
<span id="Label3"></p><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">In iOS8, Apple gives an exciting feature, Uitableview's self Sizing Cells. For developers, This is a worthwhile feature, and before iOS8, if you need to show dynamic content in uitableviewcell, you must calculate the height of the content each time, and then assign a value to Uitableview's height.</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">How do I use this feature in iOS8? There are three basic steps:</p></p> <ul> <ul> <li>Layout constraints settings for the custom cell</li> <li>Set Estimatedrowheight</li> <li>This is Uitableview's rowheight for Uitableviewautomaticdimension.</li> </ul> </ul> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">The code in the two section looks like This:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <table style="width:100%;" cellpadding="2" cellspacing="0" border="1" bordercolor="#000000"> <tbody> <tr> <td> <p><p> <span style="font-family:menlo, Monaco, ' andale Mono ', ' lucida Console ', ' Courier New ', monospace;line-height : 18.850000381469727px;vertical-align:baseline;color: #93A1A1; "><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color: #268BD2!important;">tableView </span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;" . < span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color: #268BD2!important;">estimatedRowHeigh T </span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold!important;" = < span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color: #2AA198!important;" 68.0 < span>   </span> </span></span></span></p></p> <p><p><span style="font-family:Menlo, Monaco, ‘Andale Mono‘, ‘lucida console‘, ‘Courier New‘, monospace;line-height:18.850000381469727px;vertical-align:baseline;color:#93A1A1;"> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">tableView</span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">.</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">RowHeight</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold !important;">=</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">uitableviewautomaticdimension</span> </span></p></p> </td> </tr> </tbody> </table> <br> <span style="line-height:1.5;">with <span style="line-height:1.5;">just two lines of code, your uitableview will be dynamically displayed based on the Content. The characteristics of this self-tuning unit should save you code and Time. you'll love it. </span></span> <p><p></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <span id="OSC_h4_1"><span id="OSC_h4_1"></span></span>Building a simple Demo Using the self Sizing Cell<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">There is no better way to learn it than to use a feature. A project template will be used to learn the self sizing cell. There are two labels on the custom cell that are used to display names and addresses, respectively. You can download the template application template, to do the later learning, in the template does not do any constraints settings, etc. After you run the project, it looks like this:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">As you can see, this UITableViewCell does not have a more dynamic change in the height of the content, we want to achieve the goal is that the name and address can be fully displayed, the cell can be changed dynamically according to the Content.</p></p> <span id="OSC_h4_2"><span id="OSC_h4_2"></span></span>Adding Auto Layout Constraints<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">Before iOS8, if you want to dynamically change the height, you need to consider the font, number of rows and so on some factors, and not necessarily accurate, but in iOS8, these factors can not be considered, dynamic features will help you do a lot of things.</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">After you open the project project, you can see a uitableviewcontroller in IB and select the name Label in UITABLEVIEW. Set its constraints property:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">Select the address Label setting again:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">When you are done, check the controls in IB if they are correct:<br><br></p></p> <span id="OSC_h4_3"><span id="OSC_h4_3"></span></span>Setting Estimated Row Height<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">For AutoLayout settings, you need to set the UITableView property in the Viewdidload method:</p></p> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit;vertical-align:baseline;"></span></span> <table style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;font-size:16px;"> <tbody> <tr> <td style="border:0px;font-family:inherit;font-style:inherit;font-size:16px;vertical-align:middle;"> <pre class="line-numbers"><span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">1 </span> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">2 </span> </pre> </td> <td style="border:0px;font-family:inherit;font-style:inherit;font-size:16px;vertical-align:middle;"> <pre><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> <span style="font-family:inherit ; Line-height:inherit;vertical-align:baseline;color: #268BD2!important; ">tableview </span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;" . < span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color: #268BD2!important;">estimatedRowHeigh T </span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold!important;" = < span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color: #2AA198!important;" 68.0 < span> </span></span></span></span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">tableView</span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">.</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">RowHeight</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold !important;">=</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">uitableviewautomaticdimension</span> </span></span></pre> </td> </tr> </tbody> </table> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">If you run this time, you will find that the name label and Address label are still not Adaptive. The reason is that the two label lines or the default 1 result, back to ib, set the name label and address Label line property to 0:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">Once again, the height of the cell can be changed dynamically depending on the Content.<br></p></p> <span id="OSC_h4_4"><span id="OSC_h4_4"></span></span>A bug?!<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">Do not know is not a bug, when the application first run, some cells do not automatically adapt to its content, the solution is to refresh the Tableview:</p></p> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit;vertical-align:baseline;"></span></span> <table style="margin:0px;padding:0px;border:0px;font-family:inherit;font-style:inherit;font-weight:inherit;font-size:16px;"> <tbody> <tr> <td style="border:0px;font-family:inherit;font-style:inherit;font-size:16px;vertical-align:middle;"> <pre class="line-numbers"><span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">1 </span> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">2 </span> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">3 </span> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">4 </span> <span style="font-family:inherit;font-style:inherit;font-weight:inherit;line-height:inherit; Vertical-align:baseline;color: #586E75!important; ">5 </span> </pre> </td> <td style="border:0px;font-family:inherit;font-style:inherit;font-size:16px;vertical-align:middle;"> <pre><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">Override</span></span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">func</span></span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold !important;color:#268BD2 !important;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;font-weight:bold !important;color:#268BD2 !important;">Viewdidappear</span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">(</span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#859900 !important;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#859900 !important;">animated</span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">:</span></span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">Bool</span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">)</span></span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">{</span></span> </span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> </span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">tableView</span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">.</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;color:#268BD2 !important;">Reloaddata</span> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">()</span> </span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> </span></span><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"><span style="font-family:inherit;line-height:inherit;vertical-align:baseline;"> <span style="font-family:inherit;line-height:inherit;vertical-align:baseline;">}</span> </span></span></pre> </td> </tr> </tbody> </table> <span id="OSC_h4_5"><span id="OSC_h4_5"></span></span>Dynamic Type<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">Then verify the self Sizing feature, change the text font size into the system settings, and then go back to the app to View:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">If you are normal, you will find that the contents of UITableView are changed as well, and the cell height will change with the Content:</p></p> <p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"></p></p> <span id="OSC_h4_6"><span id="OSC_h4_6"></span></span>Summary<p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;"><p style="font-family:Lato, sans-serif;font-size:16px;vertical-align:baseline;text-align:justify;color:#222222;background-color:#F8F8F8;">There are many features to be used in iOS8, which are designed to make it easier and faster to develop applications. Often it takes just a few lines of code to implement some of the effects of iOS8 that previously required a lot of code. This introduction, the complete demo can click Download: Demo download</p></p> <blockquote> <blockquote> <p style="font-family:inherit;font-style:inherit;font-weight:inherit;vertical-align:baseline;text-align:justify;">Disclaimer: This article is from appcoda, there are many places have their own elements, please understand. The level is limited, still hope not to shoot bricks!</p> </blockquote> </blockquote><p><p>Dynamically changing cell heights and IOS8 dynamic type understanding under IOS8</p></p></span>

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.