Turn-dynamically change the cell height in the UITableView

Source: Internet
Author: User

Often used in the development of the iphone application process should be regarded as uitableview, with iOS to UITableView endowed with this flexible framework structure, let it in the display list or in the layout has a certain advantage. Although UITableView is powerful, there are some problems in the development process for some complex application requirements, such as dynamically changing the cell height of the UITableView display is one of them

Actually want to change UITableView cell height is not difficult, uitableview with a rowheight attribute, use He can change the height. But the change is to change the height of all cells. If there are different content there are different cell heights, then this time rowheight seems to be powerless. However, iOS seems to take this into account, and under UITableView's Uitableviewdelegate delegate There is a delegate method that dynamically specifies the height of the cell, which is declared as follows:

-(CGFloat) TableView: (UITableView *)tableView heightforrowatindexpath: (Nsindexpath *)Indexpath

This delegate method can control the cell height returned based on the index location Indexpath. Maybe someone would think that I can implement this mandate to achieve the effect I said above? It is the use of this Commission is correct, but there are some problems when using this delegate, the following is to address these issues I in the actual development work of the approach, I hope that through the sharing of these ideas can let children's shoes less detours.

In fact, the main problem is heightforrowatindexpath the invocation time of this delegate is earlier than Cellforrowatindexpath (this method is defined in Uitableviewdatasource) we all know Cellforrowatind Expath is the method used to return UITableViewCell. So the problem is here, if my cell hasn't returned, how can I get the cell height in the Heightforrowatindexpath? Someone might say, "I can get a height by refreshing the specified cell after returning to the cell." But in fact this practice is very difficult. It is also important to note that the Heightforrowatindexpath is not able to use the UITableView Cellforrowatindexpath: Returns the Cell object. Doing so causes a stack overflow to be thrown by an infinite-level recursive call. the reason is that calling this method triggers the Heightforrowatindexpath delegate method. But it's also possible to call. The solution is to first empty the delegate, and then get to the cell and then re-assign the value. Such as:

Tableview.delegate=nil;

UITableViewCell *cell=[tableview Cellforrowatindexpath:indexpath];

tableview.delegate=self;

So what can be more convenient to achieve it? Our traditional approach is to typeset or inherit UITableViewCell in Cellforrowatindexpat. And this is inevitable, but for this dynamic change height of processing, I think it is best not to cellforrowatindexpat directly in the layout, should inherit UITableViewCell generation subclass, and then in the sub-class to do the typesetting work. Because this is related to the solution mentioned below.

First talk about my solution, in fact, the method is very simple, through the sample cell to calculate the height can be. That is, after I have defined the subclass of the cell, I define a property of the cell subclass in the Controller class or view class that contains UITableView, which is specifically used to calculate the cell height in Heightforrowatindexpath. This will return the cell's height correctly. Let's take a look at the subclass definition of cell:

@interface democell:nsobject{

UILabel *_content;

}

-(cgfloat) contentheight;

-(void) SetContent: (NSString *) content;

@end;

From the Democell above, it comes with a Uilabel object, which is to dynamically change the height based on the contents of the Uilabel. Where the Contentheight method is to return the height of the cell. SetContent is to set the content of the Uilabel and calculate the height of the Uilabel. The simple thing to do with this class is these operations. Then we define a sample cell in the controller that is dedicated to calculating the cell's height. The code is as follows:

#import "DemoCell.h"

@interface demoviewcontroller:uiviewcontroller<uitableviewdelegate,uitableviewdatasource>{

UITableView *_tableview;

Democell *_samplecell;

Nsarray *_listdata;

}

@end;

Some of the implementation code is as follows:

-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) indexpath{

This uses the sample cell to calculate the height.

NSString *content=[_listdata ObjectAtIndex:indexPath.row];

[_samplecell setcontent:content];

return [_samplecell Contentheight];

}

-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) indexpath{

Static nsstring *[email protected] "Democell";

Democell *cell= (Democell *) [TableView dequeuereusablecellwithidentifier:cellid];

if (Cell==nil) {

Cell=[[[democell alloc] Initwithstyle:uitableviewcellstyledefault reuseidentifier:cellid] autorelease];

}

[Cell Setcontent:[_listdata objectatindex IndexPath:indexPath.row];

return cell;

}

So far, we've successfully demonstrated how to dynamically change the cell's height. As soon as the content changes we call UITableView's Reloaddata method to refresh the entire list. How the cell calculates the height I am not listed, because different requirements cause this part of the implementation will not be the same. Here is just the idea to explain clearly to everyone. Concrete implementation or to the children's shoes to do their own hands-on operation.

Turn-dynamically change the cell height in the UITableView

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.