Fast Scrolling a UITableView in Monotouch

This is a port I did of the fast scrolling example here: http://blog.atebits.com/2008/12/fast-scrolling-in-tweetie-with-uitableview/

The main thing to note is the code in CustomTableViewCell.DrawContentView, which takes care of drawing the value set in the NSString Text property to the screen, as the cells come into view, while the table is scrolled.

public override void DrawContentView (RectangleF rect)
{
CGContext context = UIGraphics.GetCurrentContext ();

UIColor backgroundColor = UIColor.White;
UIColor textColor = UIColor.Black;

if (this.Highlighted) {
backgroundColor = UIColor.Clear;
textColor = UIColor.White;
}

backgroundColor.SetColor ();

context.FillRect (rect);

Point pt = new Point (50, 10);

textColor.SetColor ();

DrawString (Text, pt, _textFont);
}
}

This gets invoked via an abstract base class that derives from UITableViewCell. Actually, the base class nests another class, which subclasses UIView, that serves as the content container. It’s in that classes’ override of the Draw method that the call to DrawContentView happens.

public abstract class TableViewCellBase : UITableViewCell
{
TableViewCellContent _contentView;

public TableViewCellBase (string reuseIdentifier) : base(UITableViewCellStyle.Default, reuseIdentifier)
{
_contentView = new TableViewCellContent ();
this.Opaque = true;
this.AddSubview (_contentView);
}

public override RectangleF Frame {
get { return base.Frame; }
set {
base.Frame = value;

if (_contentView != null) {
RectangleF boundingRectangle = this.Bounds;
_contentView.Frame = boundingRectangle;
}

}
}

public override void SetNeedsDisplay ()
{
base.SetNeedsDisplay ();
if (_contentView != null)
_contentView.SetNeedsDisplay ();
}

public abstract void DrawContentView (RectangleF rect);

class TableViewCellContent : UIView
{
public TableViewCellContent () : base(RectangleF.Empty)
{
}

public override void Draw (RectangleF rect)
{
((TableViewCellBase)this.Superview).DrawContentView (rect);
}
}

}

I tried it on an iPhone 3G and the scrolling performance appeared to be very good.

Here’s the full project: http://cid-0486030770596f62.skydrive.live.com/self.aspx/Public/FastScrolling.zip

One thought on “Fast Scrolling a UITableView in Monotouch

  1. Pingback: MonoTouch.Info

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s