I have a table view with cells containing a label that varies on length. I was wondering how I could change the height of the cells based on the length of each of their labels.
YOu can use the following NSString method to get the size that the text would take up in the label, and then you can adjust your table cell height accordingly..
YOu can use the following NSString method to get the size that the text would take up in the label, and then you can adjust your table cell height accordingly..
there is a uitableview delegate method that you need to implement
Code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* theText = //first get the text that will be displayed in the cell
CGSize textSize = [theTextsizeWithFont: font constrainedToSize:CGSizeMake(280, 500)];
return textSize.height;
}
there is a uitableview delegate method that you need to implement
Code:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* theText = //first get the text that will be displayed in the cell
CGSize textSize = [theTextsizeWithFont: font constrainedToSize:CGSizeMake(280, 500)];
return textSize.height;
}
Hmm... okay so I added the CGFloat, but it said that my 'cell' wasn't declared, so I redeclared it in this new method but look at the table view image now.
Go read some of the stuff in the table view link in my signature to pick up some table view basics.
After you grasp those, then go to the blog link I already posted.
Hey thanks, I looked through your links and I'm definitely going to go back for the search controller tutorial, but I basically understand building the table view, the problem for me is working with dynamic heights on custom cells. I followed the gf cocoa tutorial for a regular table view, and it worked fine, but when I keep trying to implement the same concepts into my project with custom cells it just looks horrendous...
The custom cells work perfectly though, now I'm getting the cells to resize according to the facts label length, but the actual label won't show all of it's text, just the ellipsis...
@Brian: I followed the tutorial you linked and it works perfectly, but I can't come up on where I'm wrong when I try to make the table view sectioned and divide the cell exemples provided in sections: I've created arrays and dictionaries in the viewDidLoad method but probably I'm missing something in the display method…
I'm just using the sample code of the tutorial before coping the code I need into my app. I've tried to divide the sample cells into section but I'm missing something and my try went wrong. So, my question is: from that sample code, how do I divide the cells into sections? Can you give me some code? I've tried several ways but either the app crashes or doesn't start at all…
Well, you've obviously changed something. So knowing what you've changed will be useful. And knowing what error message you get when you crash would be helpful too.
Well, you've obviously changed something. So knowing what you've changed will be useful. And knowing what error message you get when you crash would be helpful too.
Here is my modified code: you will see I made just a few changes but when you Build&Run it starts but crashes on loading… I'm sure the solution it's really simple but I don't get it…
Code:
#import "DynamicHeightsViewController.h"
#define FONT_SIZE 14.0f
#define CELL_CONTENT_WIDTH 305.0f
#define CELL_CONTENT_MARGIN 10.0f
@implementation DynamicHeightsViewController
- (void)viewDidLoad {
[super viewDidLoad];
items = [[NSMutableArray alloc] init];
NSArray *JGDInfoDictionaryArray = [NSArray arrayWithObjects:@"Happiness is having a large, loving, caring, close-knit family in another city.\n\n\t\t-George Burns (1896 - 1996)", nil];
NSDictionary *JGDInfoDictionaryDict = [NSDictionary dictionaryWithObject:JGDInfoDictionaryArray forKey:@"Info"];
NSArray *HowToUseDictionaryArray = [NSArray arrayWithObjects:@"When I am abroad, I always make it a rule never to criticize or attack the government of my own country. I make up for lost time when I come home.\n\n\t\t-Sir Winston Churchill (1874 - 1965)", nil];
NSDictionary *HowToUseDictionaryDict = [NSDictionary dictionaryWithObject:HowToUseDictionaryArray forKey:@"Info"];
NSArray *CreditsDictionaryArray = [NSArray arrayWithObjects:@"After two years in Washington, I often long for the realism and sincerity of Hollywood.\n\n\t\t-Fred Thompson, Speech before the Commonwealth Club of California", nil];
NSDictionary *CreditsDictionaryDict = [NSDictionary dictionaryWithObject:CreditsDictionaryArray forKey:@"Info"];
[items addObject:JGDInfoDictionaryDict];
[items addObject:HowToUseDictionaryDict];
[items addObject:CreditsDictionaryDict];
}
- (void)dealloc {
[items release], items = nil;
[super dealloc];
}
#pragma mark -
#pragma mark UITableView Delegaates
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
NSDictionary *dictionary = [items objectAtIndex:section];
NSArray *array = [dictionary objectForKey:@"Info"];
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
{
return [items count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
CGFloat height = MAX(size.height, 44.0f);
return height + (CELL_CONTENT_MARGIN * 2);
}
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UILabel *label = nil;
cell = [tv dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"Cell"] autorelease];
label = [[UILabel alloc] initWithFrame:CGRectZero];
[label setLineBreakMode:UILineBreakModeWordWrap];
[label setMinimumFontSize:FONT_SIZE];
[label setNumberOfLines:0];
[label setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[label setTag:1];
[[cell contentView] addSubview:label];
}
NSString *text = [items objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
if (!label)
label = (UILabel*)[cell viewWithTag:1];
[label setText:text];
[label setFrame:CGRectMake(CELL_CONTENT_MARGIN, CELL_CONTENT_MARGIN, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))];
return cell;
}
@end
-[__NSCFDictionary sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0x5f4a950'
Yup. You aren't dealing with your data correctly. You are still trying to access it as though you have an array of strings. That is no longer the case.
P.S. As I said in the previous reply, no error occur in the building process, that's why I just copied the debugger infos. If there's something else you need, just ask.
-[__NSCFDictionary sizeWithFont:constrainedToSize:lineBreakMode:]: unrecognized selector sent to instance 0x5f4a950'
Yup. You aren't dealing with your data correctly. You are still trying to access it as though you have an array of strings. That is no longer the case.
I see. Well, I guess you need to spend more time learning and less time copy-pasting code that you don't understand.
Spend more than 30 seconds studying what those methods actually do, and how that might relate to your error message. I've given you the answers you need. See the table view link in my signature for more guidance.