开发者

UITableViewCell Dynamic Height issues

开发者 https://www.devze.com 2022-12-13 08:25 出处:网络
Ok I am working on a project, and it would be VERY helpful if I could somehow figure out how to change one (or all) of the cells so that their height is contingent on the amount of text they hold. I f

Ok I am working on a project, and it would be VERY helpful if I could somehow figure out how to change one (or all) of the cells so that their height is contingent on the amount of text they hold. I found a tutorial on the internet that made it seem quite feasible, but I just am getting stuck becuse I am copying and pasting his code right into my app, and there is an error that pops up where his code开发者_JS百科 reads:

NSString *text = [items objectAtIndex:[indexPath row]];

If there is a quick fix using this tutorial, or if anyone could explain what the items thing is all about, thatd be great


You probably should look at other examples of how tables work to see how data flows into them.

However, when you are ready here's a rough outline of how you do variable height cells:

1) Have heightForRowAtIndexPath return a custom height for each row

2) In that method, Figure out what the text for the row will be, then use the method:

- (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode

(On NSString, in the UIStringDrawingAdditions) to figure out what height to return (you need the text height plus some padding). The width is the width of the label that will hold the text, the returned CGSize holds the height the label will be when you use that text.

3) In your cell, you have to set the number of lines to 0 so that it can fill out however many are needed.

4) If you are using a custom cell, you need to make sure the label height is resized to fit the text you are placing in - the cell height will already be set based on the heightForRowAtIndexPath method. I think a plain UITableViewCell will probably have text labels resize in height along with the cell, if not you need to resize those too.

5) Set the text you are using into the label, and enjoy.

The key to understanding is that you need the height really twice, once in heightForRow (which controls cell height) and then again when you are actually setting up the text in the cell (to make sure the label is the right size).


I think your getting ahead of yourself. You need to understand the basics of Objective-C, such as arrays, strings etc as well as the basics of populating a table before you start trying to set custom row heights. I would recommend this thread to start: Howto articles of iPhone Development.

I found Beginning iPhone Development Exploring the iPhone SDK By: Dave Mark; Jeff LaMarche very useful but I had a strong background previously in Objective-C. Programming in Objective-C 2.0, Second Edition By: Stephen G. Kochan is a good reference for Objective-C. Both are available online by subscription so you don't have to shell out a $80 bucks to get started.


In this example 'items' is an NSArray that holds the values to be added as cells to the table. Often you would populate such an array in your

-(void)viewDidLoad

or

-(void)viewWillAppear:(BOOL)animated

So, once you use this method to supply your data the method in the tutorial you linked should work.

in your header file you would want

NSArray *items;

and

@property(nonatomic,retain)NSArray *items;

and in your .m file

@synthesize items;

then in either of the methods listed above, this

self.items = /* however you can build an NSArray with the data you need*/


The variable NSString* text is representing a string that you want to display in that cell. If you don't know AND that 'your app' don't know what to display, ask you the question : what do you want your app to display? What are the data?

0

精彩评论

暂无评论...
验证码 换一张
取 消