UITableViewCell height

2 views
Skip to first unread message

DavidK

unread,
Jun 5, 2009, 11:36:04 PM6/5/09
to RTFM: Development for Mobile Devices
Here's my code until Jeff posts his presumably better code.

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero
reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:[[[UILabel alloc] init] autorelease]];
}

// Set up the cell...
[[cell.contentView.subviews objectAtIndex:0] removeFromSuperview];
double d = [self tableView:table heightForRowAtIndexPath:indexPath];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10,
260, d-20)];
label.numberOfLines = 100;
label.lineBreakMode = UILineBreakModeWordWrap;
[label setFont:[UIFont boldSystemFontOfSize:[UIFont
smallSystemFontSize]]];
label.text = [descriptions objectAtIndex:indexPath.section];
[cell.contentView addSubview:label];
[label release];
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath {
CGSize aSize = [[descriptions objectAtIndex:indexPath.section]
sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont
smallSystemFontSize]] constrainedToSize:CGSizeMake(260.0, 1000.0)
lineBreakMode:UILineBreakModeWordWrap];
return aSize.height+20;
}

Jeff Hentschel

unread,
Jun 6, 2009, 2:13:49 AM6/6/09
to RTFM: Development for Mobile Devices
Here's my code. It's pretty much the same once I realized I was using
something that is SDK 3.0. I've included the stuff that's in 2.0 since
that's public.

// Add this method to the class with the tableView to change the
height for each row at an indexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath {
if (indexPath.section == sectionThatShouldBeResized) {
CGSize textSize = [textThatGoesInLabel sizeWithFont:[UIFont
systemFontOfSize:sizeOfText]
constrainedToSize:CGSizeMake(270.0, 99999999)
lineBreakMode:UILineBreakModeWordWrap];

return (textSize.height + 20 > 44) ? textSize.height + 20 : 44; //
Add twenty for funny stuff. Also, I limit the size here to 44 because
I use accessories and it looks bad if it's less than that. Change this
to fit your own needs.
}

return 44; //Otherwise, return the default size of 44
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

if (indexPath.section == sectionThatShouldBeResized) {
CellIdentifier = @"CellIdentifierOne";

UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// Err… my code can't be displayed here b/c it uses SDK 3.0 stuff.
But just make a label like david
// said and you use these 3 lines to configure it.

newLabel.font = [UIFont systemFontOfSize:kInstructionFontSize];
newLabel.lineBreakMode = UILineBreakModeWordWrap;
newLabel.numberOfLines = 0; //This allows the textLabel to have as
many lines as necessary
}

newLabel.text = textThatGoesInLabel;
return cell;
}
return nil;
}

Felix Hu

unread,
Jun 6, 2009, 1:29:51 PM6/6/09
to rtfm-development-...@googlegroups.com
Thanks guys, I'll take a look at both.
Reply all
Reply to author
Forward
0 new messages