From: http://hi.baidu.com/vim888/blog/item/6fcd6824c091c931d507421b.html
Reprinted Description: comprehend this articleArticleThe essence of the article is good, it is not necessary to stick to the difficult.
-------------------------------------------------- The following is the self-converted content -----------------------------------------------------
Uitableview is often the most used in the development of iPhone applications. With iOS, uitableview provides this flexible framework structure, it has certain advantages in display list and layout. Although uitableview is powerful, some problems may occur during the development of complex application requirements. For example, changing the cell height displayed by uitableview dynamically is one of them.
In fact, it is not difficult to change the cell height of uitableview. uitableview has a rowheight attribute and can be used to change the height. However, this change changes the height of all cells. If different contents have different cell heights, then rowheight is insufficient. However, IOS seems to have taken this into consideration. Under the uitableview uitableviewdelegate delegate, a delegate method can dynamically specify the cell height. Its declaration is as follows:
-(Cgfloat) tableview :( uitableview *)TableviewHeightforrowatindexpath :( nsindexpath *)Indexpath
This delegate method controls the returned cell height based on the index position indexpath. May someone think that I can achieve the effect I mentioned above by implementing this delegate? Yes, it is correct to use this delegate, but there will be some problems when using this delegate. The following content is to put forward the handling methods for these problems in actual development work, we hope that we can share these ideas to minimize the possibility of detours.
In fact, the main problem is that the call time of the heightforrowatindexpath delegate method is earlier than that of the cellforrowatindexpath delegate method (which is defined in uitableviewdatasource). We all know that cellforrowatindexpath is used to return uitableviewcell. So the problem is here. If no cell is returned, how can I get the cell height in heightforrowatindexpath? Some people may say that the height can be obtained after I refresh the specified cell after returning the cell. However, this practice is very difficult.Note that in heightforrowatindexpath, The cellforrowatindexpath of uitableview cannot be used to return the cell object. Otherwise, the stack overflow caused by infinite recursive calls may occur.The reason is that calling this method will triggerHeightforrowatindexpath delegate method. But it is also possible to call. The solution is to leave delegate empty and assign a value again after obtaining the cell. For example:
Tableview. Delegate = nil;
Uitableviewcell * cell = [tableview cellforrowatindexpath: indexpath];
Tableview. Delegate = self;
So what methods can be implemented more conveniently? Our traditional approach is to typeset in cellforrowatindexpat or inherit uitableviewcell. This is inevitable, but I think it is best not to directly typeset the height of this dynamic change in cellforrowatindexpat. Instead, we should inherit uitableviewcell to generate a subclass and then typeset It In The subclass. BecauseThe solution described below is related.
Let's talk about my solution. In fact, the solution is very simple. We can use the sample cell to calculate the height. That is to say, after defining the cell subclass, I define the attributes of a cell subclass in the Controller class or view class that contains the uitableview. This attribute is specifically used to calculate the cell height in heightforrowatindexpath. In this way, the height of the cell is returned correctly. Let's take a look at the cell subclass definition:
@ Interface democell: nsobject {
Uilabel * _ content;
}
-(Cgfloat) contentheight;
-(Void) setcontent :( nsstring *) content;
@ End;
From the preceding democell, it has a uilabel object. This cell dynamically changes the height based on the content of uilabel. The contentheight method returns the height of the cell. Setcontent is used to set the content of uilabel and calculate the height of uilabel. This is easy to do. Then we define a sample cell in the Controller specifically used to calculate the cell height.CodeAs 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 {
// Use 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 * cellid = @ "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 have successfully demonstrated how to dynamically change the height of cells. As long as the content changes, we can call the reloaddata method of uitableview to refresh the entire list. How to calculate the cell height is not listed by myself, because different requirements lead to different implementations of this part. Here we will give you a clear explanation of the ideas. The specific implementation requires you to do it yourself.