Skip to content

Commit

Permalink
Fixed owner drawn cell element and added a more realistic demo
Browse files Browse the repository at this point in the history
The GetCell call for the owner drawn element reused cells without changing the underlying custom element. This ment that as the user scrolled the table old cells were placed at the bottom on the grid. I modified the Get cell code to propogate the newly inserted OwnerDrawnElement down to the OwnerDrawnCellView.

If you want to see the prefix behaviour try the demo without the fix. You will see the first row data pop up at the bottom (original demo only worked because the cell content was always the same)
  • Loading branch information
davidblackuk authored and migueldeicaza committed Aug 23, 2010
1 parent 6fd43b1 commit 3ff3a6c
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 29 deletions.
47 changes: 40 additions & 7 deletions MonoTouch.Dialog/OwnerDrawnElement.cs
Expand Up @@ -37,9 +37,13 @@ public override UITableViewCell GetCell (UITableView tv)
if (cell == null)
{
cell = new OwnerDrawnCell(this, this.Style, this.CellReuseIdentifier);
cell.Update();
}

else
{
cell.Element = this;
}

cell.Update();
return cell;
}

Expand All @@ -53,18 +57,38 @@ class OwnerDrawnCell : UITableViewCell

public OwnerDrawnCell(OwnerDrawnElement element, UITableViewCellStyle style, string cellReuseIdentifier) : base(style, cellReuseIdentifier)
{
view = new OwnerDrawnCellView(element);
ContentView.Add(view);
Element = element;
}

public OwnerDrawnElement Element
{
get {
return view.Element;
}
set {
if (view == null)
{
view = new OwnerDrawnCellView (value);
ContentView.Add (view);
}
else
{
view.Element = value;
}
}
}



public void Update()
{
SetNeedsDisplay();
view.SetNeedsDisplay();
}

public override void LayoutSubviews ()
public override void LayoutSubviews()
{
base.LayoutSubviews ();
base.LayoutSubviews();

view.Frame = ContentView.Bounds;
}
Expand All @@ -76,7 +100,16 @@ class OwnerDrawnCellView : UIView

public OwnerDrawnCellView(OwnerDrawnElement element)
{
this.element = element;
this.element = element;
}


public OwnerDrawnElement Element
{
get { return element; }
set {
element = value;
}
}

public void Update()
Expand Down
102 changes: 80 additions & 22 deletions Sample/DemoOwnerDrawnElement.cs
Expand Up @@ -4,29 +4,37 @@
using MonoTouch.CoreGraphics;
using MonoTouch.UIKit;
using MonoTouch.Dialog;
using MonoTouch.Foundation;

namespace Sample
{
public partial class AppDelegate
{
private const string SmallText="Lorem ipsum dolor sit amet";
private const string MediumText = "Integer molestie rhoncus bibendum. Cras ullamcorper magna a enim laoreet";
private const string LargeText = "Phasellus laoreet, massa non cursus porttitor, sapien tellus placerat metus, vitae ornare urna mi sit amet dui.";
private const string WellINeverWhatAWhopperString="Nulla mattis tempus placerat. Curabitur euismod ullamcorper lorem. Praesent massa magna, pulvinar nec condimentum ac, blandit blandit mi. Donec vulputate sapien a felis aliquam consequat. Sed sit amet libero non sem rhoncus semper sed at tortor.";


public void DemoOwnerDrawnElement ()
{
var root = new RootElement("Owner Drawn") {
new Section() {
new SampleOwnerDrawnElement("Element 1"),
new SampleOwnerDrawnElement("Element 2"),
new SampleOwnerDrawnElement("Element 3"),
new SampleOwnerDrawnElement("Element 4"),
new SampleOwnerDrawnElement("Element 5"),
new SampleOwnerDrawnElement("Element 6"),
new SampleOwnerDrawnElement("Element 7"),
new SampleOwnerDrawnElement("Element 8"),
new SampleOwnerDrawnElement("Element 9"),
new SampleOwnerDrawnElement("Element 10"),
new SampleOwnerDrawnElement("000 - "+SmallText, DateTime.Now, "David Black"),
new SampleOwnerDrawnElement("001 - "+MediumText, DateTime.Now - TimeSpan.FromDays(1), "Peter Brian Telescope"),
new SampleOwnerDrawnElement("002 - "+LargeText, DateTime.Now - TimeSpan.FromDays(3), "John Raw Vegitable"),
new SampleOwnerDrawnElement("003 - "+SmallText, DateTime.Now - TimeSpan.FromDays(5), "Tarquin Fintimlinbinwhinbimlim Bus Stop F'tang F'tang Ole Biscuit-Barrel"),
new SampleOwnerDrawnElement("004 - "+WellINeverWhatAWhopperString, DateTime.Now - TimeSpan.FromDays(9), "Kevin Phillips Bong"),
new SampleOwnerDrawnElement("005 - "+LargeText, DateTime.Now - TimeSpan.FromDays(11), "Alan Jones"),
new SampleOwnerDrawnElement("006 - "+MediumText, DateTime.Now - TimeSpan.FromDays(32), "Mrs Elsie Zzzzzzzzzzzzzzz"),
new SampleOwnerDrawnElement("007 - "+SmallText, DateTime.Now - TimeSpan.FromDays(45), "Jeanette Walker"),
new SampleOwnerDrawnElement("008 - "+MediumText, DateTime.Now - TimeSpan.FromDays(99), "Adrian Blackpool Rock Stoatgobblerk"),
new SampleOwnerDrawnElement("009 - "+SmallText, DateTime.Now - TimeSpan.FromDays(123), "Thomas Moo"),
}
};

root.UnevenRows = true;
var dvc = new DialogViewController (root, true);

navigation.PushViewController (dvc, true);
}
}
Expand All @@ -39,11 +47,16 @@ public void DemoOwnerDrawnElement ()
public class SampleOwnerDrawnElement : OwnerDrawnElement
{
CGGradient gradient;
private UIFont subjectFont = UIFont.SystemFontOfSize(10.0f);
private UIFont fromFont = UIFont.BoldSystemFontOfSize(14.0f);
private UIFont dateFont = UIFont.BoldSystemFontOfSize(14.0f);

public SampleOwnerDrawnElement (string text) : base(UITableViewCellStyle.Default, "sampleOwnerDrawnElement")

public SampleOwnerDrawnElement (string text, DateTime sent, string from) : base(UITableViewCellStyle.Default, "sampleOwnerDrawnElement")
{
this.Text = text;

this.Subject = text;
this.From = from;
this.Sent = FormatDate(sent);
CGColorSpace colorSpace = CGColorSpace.CreateDeviceRGB();
gradient = new CGGradient(
colorSpace,
Expand All @@ -52,26 +65,71 @@ public SampleOwnerDrawnElement (string text) : base(UITableViewCellStyle.Default
new float[] { 0, 1 } );
}

public string Text
public string Subject
{
get; set;
}

public string From
{
get; set;
}

public string Sent
{
get; set;
}


public string FormatDate (DateTime date)
{
get;set;

if (DateTime.Today == date.Date) {
return date.ToString ("hh:mm");
} else if ((DateTime.Today - date.Date).TotalDays < 7) {
return date.ToString ("ddd hh:mm");
} else
{
return date.ToShortDateString ();
}
}

public override void Draw (RectangleF bounds, CGContext context, UIView view)
{
UIColor.White.SetFill();
context.FillRect(bounds);
UIColor.White.SetFill ();
context.FillRect (bounds);

context.DrawLinearGradient (gradient, new PointF (bounds.Left, bounds.Top), new PointF (bounds.Left, bounds.Bottom), CGGradientDrawingOptions.DrawsAfterEndLocation);

context.DrawLinearGradient(gradient, new PointF(bounds.Left, bounds.Top), new PointF(bounds.Left, bounds.Bottom), CGGradientDrawingOptions.DrawsAfterEndLocation);
UIColor.Black.SetColor ();
view.DrawString(this.From, new RectangleF(10, 5, bounds.Width/2, 10 ), fromFont, UILineBreakMode.TailTruncation);

UIColor.Black.SetColor();
UIColor.Brown.SetColor ();
view.DrawString(this.Sent, new RectangleF(bounds.Width/2, 5, (bounds.Width/2) - 10, 10 ), dateFont, UILineBreakMode.TailTruncation, UITextAlignment.Right);

view.DrawString(this.Text, new RectangleF(10, 15, bounds.Width - 20, bounds.Height - 30), UIFont.BoldSystemFontOfSize(14.0f), UILineBreakMode.TailTruncation);
UIColor.DarkGray.SetColor();
view.DrawString(this.Subject, new RectangleF(10, 30, bounds.Width - 20, TextHeight(bounds) ), subjectFont, UILineBreakMode.WordWrap);
}

public override float Height (RectangleF bounds)
{
return 44.0f;
var height = 40.0f + TextHeight (bounds);
return height;
}

private float TextHeight (RectangleF bounds)
{
SizeF size;
using (NSString str = new NSString (this.Subject))
{
size = str.StringSize (subjectFont, new SizeF (bounds.Width - 20, 1000), UILineBreakMode.WordWrap);
}
return size.Height;
}

public override string ToString ()
{
return string.Format (Subject);
}
}
}
Expand Down

0 comments on commit 3ff3a6c

Please sign in to comment.