Luke Tupper Consulting

Luke Tupper Consulting Blog

Doing Big Text in UITableView

September6

If you need to display differing size text in a UITableView here is some sample code that uses standard UITableViewCells and changes the height depending on the size of the Table View (handles rotation ok).

The main sections of code is the heightForRowAtIndexPath:

NSString *bigText = [self.bigTexts objectAtIndex:indexPath.row];
CGSize textSize = [bigText sizeWithFont:self.labelFont
constrainedToSize:CGSizeMake(tableView.frame.size.width - 40.0, MAXFLOAT) //40 should be the margin needed for controls etc.
lineBreakMode:UILineBreakModeWordWrap];
return textSize.height;

And the initialisation of the TableViewCell:

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
cell.textLabel.text = (NSString *)[self.bigTexts objectAtIndex:indexPath.row];
cell.textLabel.font = self.labelFont;

return cell;

I have uploaded the complete project for those who want to run it straight out of the box.

Simple iPhone Table Example

July19

Unless you are building a custom UI (like a game) on the iPhone you probably spend a lot of time working with UITableVews.

I often found it difficult to find good simple examples of iPhone TableView code.

This first sample shows three different uses of UITableView:

1) Very basic table
2) Table that can be resorted.
3) Table that can be resorted with the first and last rows locked in place.

Click on the plain/grouped segment control to choose which style of table you want.

Table Sample

Cocoa Quick Tip: Insert/Delete Icons not displaying on Table

July7

Had a bit of an issue that took me a bit of head scratching. I had a UITableView which worked perfectly in iPhone API 2.2 but in 3.0 the insert/delete icons weren’t being displayed. Reworked and checked all the code but nothing worked.

It seems if you set the editing flag to YES when creating the tableViewCell’s, the insert/delete icon applied by the table delegate (editingStyleForRowAtIndexPath) isn’t displayed. Removing the call to cell.editing seems to fix the problem. This seems to be one of those things that is slightly different between iPhone OS 2.2 and 3.0.