Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created November 30, 2011 22:35
Show Gist options
  • Save nicwise/1411468 to your computer and use it in GitHub Desktop.
Save nicwise/1411468 to your computer and use it in GitHub Desktop.
InvoiceElement
public class InvoiceElement : StyledStringElement, IElementSizing
{
private Contact contact;
private Invoice invoice;
static NSString skey = new NSString("InvoiceElement");
public InvoiceElement(Invoice invoice, Contact contact, NSAction ontapped) : base("", ontapped)
{
this.contact = contact;
this.invoice = invoice;
}
readonly float cellHeight = 44f; //standard height. You can make it bigger or smaller, tho smaller means fingers miss easier.
public float GetHeight (UITableView tableView, NSIndexPath indexPath)
{
return cellHeight;
}
public override UITableViewCell GetCell (UITableView tv)
{
var cell = tv.DequeueReusableCell (skey);
UILabel contactNameLabel = null;
UILabel invoiceAmountLabel = null;
UILabel dueOnLabel = null;
UILabel invoiceNumberLabel = null;
UILabel extraInfoLabel = null;
// if it's null, we need to make a new one. If not, we can ask it for our various views by Tag
if (cell == null){
cell = new UITableViewCell (UITableViewCellStyle.Default, skey);
cell.SelectionStyle = UITableViewCellSelectionStyle.Blue;
cell.BackgroundColor = UIColor.Clear;
//Not sure why I have a clear background and a clear view. But it is....
cell.BackgroundView = new UIView { Frame = new RectangleF(0,0,0,0) };
//useful for debugging, "where the hell is my view?"
//cell.ContentView.BackgroundColor = UIColor.Red;
var frame = cell.ContentView.Frame;
frame.Height = cellHeight;
cell.ContentView.Frame = frame;
//thinking about it, cell.ContentView.Clear() might be an idea here... it works tho :)
float width = 0;
// Make a label, set it's properties, add it to the ContentView. Repeat.
invoiceAmountLabel = new UILabel();
invoiceAmountLabel.Frame = new RectangleF(220,5, 100,21);
invoiceAmountLabel.TextAlignment = UITextAlignment.Right;
invoiceAmountLabel.Font = UIFont.BoldSystemFontOfSize(14f);
invoiceAmountLabel.TextColor = StockImages.DetailColor;
invoiceAmountLabel.BackgroundColor = UIColor.Clear;
invoiceAmountLabel.AdjustsFontSizeToFitWidth = false;
invoiceAmountLabel.Tag = 12;
invoiceAmountLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(invoiceAmountLabel);
contactNameLabel = new UILabel();
contactNameLabel.Frame = new RectangleF(10,5, 300,21);
contactNameLabel.Font = UIFont.BoldSystemFontOfSize(18f);
contactNameLabel.Tag = 10;
contactNameLabel.BackgroundColor = UIColor.Clear;
contactNameLabel.AdjustsFontSizeToFitWidth = true;
contactNameLabel.MinimumFontSize = 14f;
contactNameLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin ;
cell.ContentView.AddSubview(contactNameLabel);
dueOnLabel = new UILabel();
dueOnLabel.Frame = new RectangleF(10,25, 300,21);
dueOnLabel.Font = UIFont.SystemFontOfSize(12f);
dueOnLabel.TextColor = StockImages.DetailColor;
dueOnLabel.BackgroundColor = UIColor.Clear;
dueOnLabel.AdjustsFontSizeToFitWidth = true;
dueOnLabel.MinimumFontSize = 12f;
dueOnLabel.TextAlignment = UITextAlignment.Left;
dueOnLabel.Tag = 11;
dueOnLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(dueOnLabel);
invoiceNumberLabel = new UILabel();
invoiceNumberLabel.Frame = new RectangleF(10,25, 300,21);
invoiceNumberLabel.Font = UIFont.SystemFontOfSize(12f);
invoiceNumberLabel.TextColor = StockImages.DetailColor;
invoiceNumberLabel.BackgroundColor = UIColor.Clear;
invoiceNumberLabel.AdjustsFontSizeToFitWidth = true;
invoiceNumberLabel.MinimumFontSize = 5f;
invoiceNumberLabel.TextAlignment = UITextAlignment.Right;
invoiceNumberLabel.Tag = 13;
invoiceNumberLabel.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleLeftMargin;
cell.ContentView.AddSubview(invoiceNumberLabel);
} else {
contactNameLabel = cell.ViewWithTag(10) as UILabel;
dueOnLabel = cell.ViewWithTag(11) as UILabel;
invoiceAmountLabel = cell.ViewWithTag(12) as UILabel;
invoiceNumberLabel = cell.ViewWithTag(13) as UILabel;
}
cell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
//Now assign the data from the datamodel, and then resize everything to fit, as the ContactNameLabel
// will resize to allow the invoiceAmountLabel to not resize - gotta show the amount :)
contactNameLabel.Text = contact.DisplayName;
dueOnLabel.TextColor = StockImages.DetailColor;
switch (invoice.Status)
{
case "Overdue":
dueOnLabel.TextColor = UIColor.Red;
dueOnLabel.Text = "Overdue. Due {0:0} days ago".Fmt((DateTime.Now - invoice.DueOn).TotalDays);
break;
case "Paid":
dueOnLabel.Text = "Paid";
break;
case "Refunded":
dueOnLabel.Text = "Refunded";
break;
case "Draft":
dueOnLabel.Text = "Draft.";
break;
default:
dueOnLabel.Text = "Due in {0:0} days".Fmt((invoice.DueOn - DateTime.Now).TotalDays);
break;
}
invoiceAmountLabel.Text = Util.FormatValueAsCurrency(invoice.NetValue + invoice.SalesTaxValue, invoice.Currency);
invoiceNumberLabel.Text = invoice.Reference;
float amountWidth = new NSString(invoiceAmountLabel.Text).StringSize(invoiceAmountLabel.Font).Width;
float contactHeight = new NSString(contactNameLabel.Text).StringSize(contactNameLabel.Font).Height+10;
float invoiceNumberWidth = new NSString(invoiceNumberLabel.Text).StringSize(invoiceNumberLabel.Font).Width;
float frameWidth = cell.ContentView.Bounds.Width-20;
invoiceAmountLabel.Frame = new RectangleF(frameWidth-amountWidth, 0, amountWidth+ 10, contactHeight);
contactNameLabel.Frame = new RectangleF(10, 0, frameWidth-10-amountWidth, contactHeight);
invoiceNumberLabel.Frame = new RectangleF(frameWidth - invoiceNumberWidth, contactHeight - 10, invoiceNumberWidth+10, 21);
dueOnLabel.Frame = new RectangleF(10,contactHeight-10,frameWidth - invoiceNumberWidth,21);
return cell;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment