iOS edge learning-customizing the Non-equal-height cell
Source: Internet
Author: User
<span id="Label3"></p><p><p>first, the use of Xib or storyboard custom non-equal high cell implementation of the same way, here is a brief introduction to the implementation of the Xib file method</p></p><p><p><1.1> Create a subclass that inherits from UITableViewCell such as Chaosweibocell</p></p><p><p><1.2> <span style="font-size: 16px; color: #ff0000;"> <strong>Add a cellheight attribute to the model to hold the corresponding cell height</strong></span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;"><span style="font-size: 16px;"><strong>#import <UIKit/UIKit.h>//modify this to UIKit frame</strong></span> @interface chaosweibo:nsobject//model class/** body */@property ( Nonatomic,strong) nsstring *text;/** Avatar */@property (nonatomic,strong) nsstring *icon;/** map */@property (nonatomic, Strong) nsstring *picture;/** user name */@property (nonatomic,strong) nsstring *name;/** VIP */@property (nonatomic,assign, Getter=isvip) BOOL vip;//cgfloat inside the CG framework NSString * Inside the foundation framework, the Uikit framework contains the height of the/** cell for both frames *<strong><span style="font-size: 16px;">/@property ( Nonatomic,assign) cgfloat cellheight;</span></strong> Provides a class method for converting dictionary data into model data + (instancetype) weibowithdict: (nsdictionary *) dict; @end</pre></pre><p><p> </p></p><p><p><2> Create a xib file (the file name is suggested to be the same as the Cell's class name), such as Chaosweibocell.xib</p></p><p><p><2.1> dragging a UITableViewCell out</p></p><p><p><2.2> Modify the Cell's class to Chaosweibocell</p></p><p><p><2.3> set up a Cell's reuse identity</p></p><p><p><2.4> Add child controls to the cell that need to be used</p></p><p><p><3> in the Controller</p></p><p><p><3.1> using registernib ... method to register the Xib file</p></p><p><p><3.2> find the cell using the reuse identity (if you do not register the Xib file, you will need to manually load the Xib File)</p></p><p><p><3.3> passing model data to cell</p></p><p><p> <span style="color: #ff0000; font-size: 16px;"><strong><3.4> implement Tableview:estimatedheightforrowatindexpath in Controller: method, return an estimate height, for example 200</strong></span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">Give the cell an estimated height//the meaning of this method is: without this method, the execution order of the program is 1, Heightforrowatindexpath: method to get the cell height (height is inaccurate, there are several to get a few cell height, consume Memory) 2, Then cellforrowatindexpath: the method creates a cell//based on an inaccurate height with this method the program executes Cellforrowatindexpath according to the approximate height: method, Then execute Heightforrowatindexpath: the method returns the True cell Height-(cgfloat) tableView: (uitableview *) TableView Estimatedheightforrowatindexpath: (nsindexpath *) indexpath{ return 200;} @end</pre></pre><p><p> <span style="color: #ff0000; font-size: 16px;"><strong><3.5> implementing Tableview:heightforrowatindexpath in the Controller: method, returns the true height of the cell (cellheight attribute in the Model)</strong></span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">-(cgfloat) tableView: (uitableview *) tableView heightforrowatindexpath: (nsindexpath *) indexpath{ Chaosweibo * Weibo = self.weiboarray[indexpath.row]; Return weibo.cellheight;}</pre></pre><p><p> </p></p><p><p><4> in the Chaosweibocell</p></p><p><p><4.1> connecting child controls in Xib to class extensions</p></p><p><p><4.2> needs to provide a model property, override the set method, set the Model property on the child control in this method</p></p><p><p> <span style="font-size: 16px;"><strong><span style="color: #ff0000;">and call the [self layoutifneed] method in the set method to force the layout, and then calculate the Cellheight property value of the model</span></strong></span></p></p><pre class="brush:objc;gutter:true;">-(void) setweibo: (chaosweibo *) weibo{//override Set method the code _weibo = weibo; Set Avatar Self.headView.image = [UIImage imageNamed:weibo.icon]; Set Nickname Self.nameLabel.text = weibo.name; Set the membership flag if (weibo.isvip) {self.vipView.hidden = No;//re-set The Hidden property to no, because the cell is recycled through the cache pool, and in case it was hidden, there is no such sentence. Self.vipView.image = [UIImage imagenamed:@ "vip"]; Self.nameLabel.textColor = [uicolor orangecolor]; } Else{self.vipView.hidden = YES; Self.nameLabel.textColor = [uicolor whitecolor]; }//set Body Content Self.contentLabel.text = weibo.text; If (weibo.picture) {self.iconView.hidden = NO;//self.iconView.image = [UIImage ImageNamed:weibo.pictur e]; } Else {self.iconView.hidden = YES; }<span style="font-size: 16px;"><span style="font-size: 16px;"><strong>//the cell that binds the data is forced to be laid out [self layoutifneeded];</strong></span></span> <strong>// <strong>Calculate the height of the cell if (self.iconView.hidden) {///no mapping is in case weibo.cellheight = Cgrectgetmaxy (self.contentLabel.frame ) + 10; } else {//there is a case of Weibo.cellheight = Cgrectgetmaxy (self.iconView.frame) + 10; }</strong></strong>}</pre><p><p> </p></p><p><p><4.3> can also encapsulate the code created to get the cell (for example, cellwithtableview: method).</p></p><p><p><span style="color: #ff0000; font-size: 16px;"><strong><5> Note that the problem is the width of the label, due to the Layoutifneed method, the system can sometimes calculate the height of the actual discrepancy. Because the height of the system is calculated according to the text Width.</strong></span></p></p><p><p><span style="color: #ff0000; font-size: 16px;"><strong>workaround, by calling the Method:</strong></span></p></p><pre class="brush:objc;gutter:true;"><pre class="brush:objc;gutter:true;">This method is the method that the custom control calls after the initialization of the Xib file, This method solves the height of the multi-line label Calculation-(void) awakefromnib{ //set the maximum width of the label text per line // The height of the label is guaranteed to be the same as the displayed effect self.contentLabel.preferredMaxLayoutWidth = [uiscreen mainscreen].bounds.size.width- 20;}</pre></pre><p><p> </p></p><p><p></p></p><p><p> iOS edge learning-customizing a non-equal-height cell </p> </p></span>
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