forked from B-Sides/ICB_SmarterTableViewCell
-
Notifications
You must be signed in to change notification settings - Fork 1
A recipe for making your UITableView cells smart enough to change their UILabels number of lines and cell height based on the text input to them.
isaacueca/ICB_SmarterTableViewCell
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
****** HOW TO USE ****** This is a simple project containing a UITableViewController. The controller lines to ensure that whatever content is placed as the text for the textLabel and detailTextLabel is displayed fully by setting the numberOfLines property of the labels appropriately based on the hight of the text. The controller also contains the code needed to have the height for each cell be appropriate based on the summed height of both the textLabel and detailTextLabel. Here is the meat of the project. // Customize the appearance of table view cells. - (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 = [[data objectAtIndex:indexPath.row] objectForKey:@"title"]; cell.textLabel.font = [UIFont boldSystemFontOfSize:18]; cell.textLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0); cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"description"]; cell.detailTextLabel.font = [UIFont systemFontOfSize:14]; cell.detailTextLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0); return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *titleString = [[data objectAtIndex:indexPath.row] objectForKey:@"title"]; NSString *detailString = [[data objectAtIndex:indexPath.row] objectForKey:@"description"]; CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap]; return detailSize.height+titleSize.height; } The MIT License Copyright (c) 2010 ELC Technologies Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
A recipe for making your UITableView cells smart enough to change their UILabels number of lines and cell height based on the text input to them.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published