views:

561

answers:

4

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 am not a very seasoned iphone developer, and so right now, i have just been swimming through a bunch of code. 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 reads:

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

Because my app doesn't know what items is. And frankly neither do I! I think I've placed his code in the right spots, but not sure. If there is a quick fix using this tutorial, or if anyone could explain what the items thing is all about, thatd be great

+1  A: 

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*/
Joe Cannatti
A: 

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?

gcamp
+2  A: 

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.

TechZen
+3  A: 

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).

Kendall Helmstetter Gelner