- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{ return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section { return 1;}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @”Cell”;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier
:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; }
cell.textLabel.text = @”Shakespeare’s Sonnet 1: From Fairest Creatures
We Desire Increase”; cell.detailTextLabel.text = @”We want all beautiful
creatures to reproduce themselves so…”; return cell;}
Some day everyone will need to show more than one text line in UITableViewCell. It can contain a title and a long description, but usually looks like that at the right:
f course it’d be great to switch certain UITableViewCell’s property and get the result, but it’s required to code a little to get a solution.
Firstly create a content: let’s take several Shakespeare Sonnets in Modern English:
static NSArray *titles;static NSArray *subtitles;
- (void)viewDidLoad { [super viewDidLoad];
if (!titles) titles = [[NSArray arrayWithObjects:
@"Shakespeare's Sonnet 1:
From Fairest Creatures We Desire Increase",
@"Shakespeare's Sonnet 2: When Forty
Winters Shall Besiege Thy Brow",
@"Shakespeare's Sonnet 3: Look In Thy Glass,
And Tell The Face Thous Viewest", nil] retain];
if (!subtitles) subtitles =
[[NSArray arrayWithObjects:@"We want all
beautiful creatures to reproduce
themselves so that beauty’s flower
will not die out; but as an old man dies in time,
he leaves a young heir to carry on his memory.",
@"When forty winters have attacked
your brow and wrinkled your beautiful skin,
the pride and impressiveness of your youth, so
much admired by everyone now, will be have
become a worthless, tattered weed.",
@"Look in your mirror and tell the face
you see that it's time it should create another
If you do not renew yourself you would be
depriving the world, and stop some woman from
becoming a mother.",nil] retain];}
Secondly create constants for minimal width
and height of cells, for fonts of title and
subtitle, and create get-methods for fonts,
And method which a UITableViewCell with
defined parameters.
#define CONST_Cell_height 44.0f#define
CONST_Cell_width 270.0f
#define CONST_textLabelFontSize
17#define CONST_detailLabelFontSize 15
static UIFont *subFont;static UIFont *titleFont;
- (UIFont*) TitleFont;{ if (!titleFont) titleFont =
[UIFont boldSystemFontOfSize:CONST_
textLabelFontSize]; return titleFont;}
- (UIFont*) SubFont;{ if (!subFont) subFont = [UIFont
systemFontOfSize:CONST_
detailLabelFontSize]; return subFont;}
- (UITableViewCell*) CreateMultilinesCell :
(NSString*)cellIdentifier{ UITableViewCell
*cell = [[[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle
reuseIdentifier:cellIdentifier] autorelease];
cell.textLabel.numberOfLines = 0; cell.textLabel.font = [self TitleFont];
cell.detailTextLabel.numberOfLines = 0; cell.detailTextLabel.font =
[self SubFont];
return cell;}
Now make a method which returns a height of cell with two input strings.
Output height
is calculated by constraining to certain size and previously defined fonts:
- (int) heightOfCellWithTitle
NSString*)titleText
andSubtitle:(NSString*)subtitl
eText{ CGSize titleSize = {0, 0}; CGSize subtitleSize = {0, 0};
if (titleText && ![titleText isEqualToString:@""])
titleSize = [titleText sizeWithFont:
[self TitleFont] constrainedToSize:CGSizeMake(CONST_Cell_width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
if (subtitleText && ![subtitleText isEqualToString:@""]) subtitleSize =
[subtitleText
sizeWithFont:[self SubFont] constrainedToSize:CGSizeMake
(CONST_Cell_width, 4000)
lineBreakMode:UILineBreakModeWordWrap];
return titleSize.height + subtitleSize.height;}
Lastly we need to change UITableViewController's standard methods and create
"heightForRowAtIndexPath". Number of rows and
"heightForRowAtIndexPath" depend of the content.
"cellForRowAtIndexPath" must return our special multilined UITableViewCell.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1;}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section
{ return MIN([titles count], [subtitles count]);}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
CellIdentifier];
if (cell == nil) { cell = [self CreateMultilinesCell:CellIdentifier];
}
cell.textLabel.text = [titles objectAtIndex:indexPath.row];
cell.detailTextLabel.text =
[subtitles objectAtIndex:indexPath.row]; return cell;}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{ NSString *title = [titles objectAtIndex:indexPath.row]; NSString *subtitle =
[subtitles objectAtIndex:indexPath.row];
int height = 10 + [self heightOfCellWithTitle:title andSubtitle:subtitle];
return
(height < CONST_Cell_height ? CONST_Cell_height : height);}
Now all including Shakespeare must be satisfied, because our
UITableViewController
renders sonnets with right height.
But this is just a sample solution, don't stop on it. Try to add UITableViewCell's
accessoryType or UIInterfaceOrientations and you will need to add options to
"heightOfCellWithTitle".
Also you can require to support different versions of SDK, it also kind of
challenge and a topic for separate post. Just ask for and you will get it.
Please load sources from GitHub or git directrly
"git clone git://github.com/slatvick/Alterplay-iPhone-dev-tips.git".
O

Now all including Shakespeare must be satisfied, because our UITableViewController renders sonnets with right height.
But this is just a sample solution, don’t stop on it. Try to add UITableViewCell’s accessoryType or UIInterfaceOrientations and you will need to add options to “heightOfCellWithTitle”.
Also you can require to support different versions of SDK, it also kind of challenge and a topic for separate post. Just ask for and you will get it.
Please load sources from GitHub or git directrly “git clone git://github.com/slatvick/Alterplay-iPhone-dev-tips.git“.