bdunagan

Brian Dunagan

December 8 2008
UITextView in a UITableView on the iPhone

While writing FilePile, I wanted to use a UITextView in a UITableView, both subclasses of UIScrollView. The file information view needed a preview section and a list of attributes, so the two classes worked well together for previewing a text file and showing its file attributes. However, the UITextView was regularly blank, on the iPhone Simulator and the device. Scrolling the view would cause its contents to appear. This UI bug was especially problematic when I had a text preview that didn’t stretch beyond the frame. It didn’t scroll, so it wouldn’t appear just by touching it; the view was just blank until it was drawn again. See the screenshot below.

The problem is easily reproducible with Apple’s UICatalog. Just make the following two changes.

  • TextViewController.m:86: change “myTableView.scrollEnabled = NO;” to “myTableView.scrollEnabled = YES;”</li>
  • TextViewController.m:146 change “return 1;” to “return 10;”</li>

Launch the project, and click on “TextView”. Slowly scroll down the screen, and you’ll see that the contents of the UITextViews don’t consistently appear. I wrote a quick sample project to demonstrate it. You can find it on Google Code or download it as a zip file. Below is a short movie showing the problem.

</embed>

I filed a bug (rdar://6429549) with Apple about the issue, but in the mean time, I found a couple ways to deal with it: reuse and setNeedsLayout. UICatalog doesn’t reuse the UITextView in TextViewController. Understandable given UICatalog only used the view once, but for these purposes, reuse is a good idea. This change cuts down on the number of UITextViews that are blank. Now when I scroll one of the blank views, that reused view appears correctly from then on, implying the problem was in initialization.

Still, reuse doesn’t solve my original problem. I only had a single UITextView in FilePile’s file view, and I want it to always show up, without requiring a scroll event. Fortunately, a simple NSTimer in the UITableView’s datasource method solves the issue. In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath, just call [NSTimer scheduledTimerWithTimeInterval:.5 target:cell selector:@selector(setNeedsLayout) userInfo:nil repeats:NO]; when the cell is a UITextView (or UIScrollView subclass). Below is the full method.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *MyTextViewId = @"MyTextViewId";
	static NSString *MyTableCellId = @"MyTableCellId";
	NSInteger section = [indexPath section];

	UITableViewCell *cell = nil;
	if (section % 2 == 1)
	{
		// It's important to reuse the UITextView.
		// cell = [tableView dequeueReusableCellWithIdentifier:@"MyTextViewId"];
		if (cell == nil)
		{
			cell = [[[BDTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyTextViewId"] autorelease];

			UITextView *textView = [[[UITextView alloc] initWithFrame:CGRectZero] autorelease];
			textView.text = @"This is a UITextView. These contents don't consistently appear because of a UI issue with a UIScrollView in a UIScrollView. This is a UITextView. These contents don't consistently appear because of a UI issue with a UIScrollView in a UIScrollView.";
			((BDTableViewCell *)cell).textView = textView;

			// Fire an NSTimer in half a second to update the cell's layout. That will make the UITextView appear.
			[NSTimer scheduledTimerWithTimeInterval:.5 target:cell selector:@selector(setNeedsLayout) userInfo:nil repeats:NO];
		}
	}
	else
	{
		cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableCellId"];
		if (cell == nil)
		{
			cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyTableCellId"] autorelease];
			cell.text = @"Just a normal UITableViewCell";
		}
	}

	// Set up the cell
	return cell;
}

With that code in place, here is a movie of the result.

</embed>

UPDATE: I filed this bug under rdar://6429549. Of course, it was a duplicate and rolled under rdar://6201360.

PackageMaker tips ibtool: Localization Made Easy
LinkedIn GitHub Email