Skip to content

Instantly share code, notes, and snippets.

@Redth
Created May 21, 2010 15:42
Show Gist options
  • Save Redth/408995 to your computer and use it in GitHub Desktop.
Save Redth/408995 to your computer and use it in GitHub Desktop.
using System;
using System.Drawing;
using MonoTouch.CoreFoundation;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace FbGroups
{
public class LoadMoreElement : MonoTouch.Dialog.Element
{
public string NormalCaption
{
get;set;
}
public string LoadingCaption
{
get;set;
}
NSAction tapped = null;
UITableViewCell cell;
UIActivityIndicatorView activityIndicator;
UILabel caption;
UITableView tableView;
UIFont font;
public LoadMoreElement (string normalCaption, string loadingCaption, NSAction tapped, UIFont font, UIColor textColor) : base("")
{
this.NormalCaption = normalCaption;
this.LoadingCaption = loadingCaption;
this.tapped = tapped;
this.font = font;
cell = new UITableViewCell(UITableViewCellStyle.Default, "loadMoreElement");
activityIndicator = new UIActivityIndicatorView();
activityIndicator.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray;
activityIndicator.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
caption = new UILabel();
caption.Font = font;
caption.Text = this.NormalCaption;
caption.TextColor = textColor;
caption.AutoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
cell.ContentView.AddSubview(caption);
cell.ContentView.AddSubview(activityIndicator);
}
public override UITableViewCell GetCell (UITableView tv)
{
tableView = tv;
Layout();
return cell;
}
public override void Selected (MonoTouch.Dialog.DialogViewController dvc, UITableView tableView, NSIndexPath path)
{
tableView.DeselectRow(path, true);
caption.Text = this.LoadingCaption;
activityIndicator.Hidden = false;
activityIndicator.StartAnimating();
Layout();
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(Tapped));
}
void Tapped(object state)
{
if (tapped != null)
tapped();
FinishedLoading();
}
void Layout()
{
var size = tableView.StringSize(caption.Text, font, tableView.Frame.Width, UILineBreakMode.TailTruncation);
float center = tableView.Frame.Width / 2;
if (activityIndicator.IsAnimating)
{
activityIndicator.Frame = new RectangleF(center - (size.Width / 2) - 24, 10, 24, 24);
caption.Frame = new RectangleF(activityIndicator.Frame.X + 35, 0, size.Width, 45);
}
else
{
caption.Frame = new RectangleF(center - (size.Width / 2), 0, size.Width, 45);
}
}
void FinishedLoading()
{
this.cell.BeginInvokeOnMainThread(delegate {
activityIndicator.StopAnimating();
activityIndicator.Hidden = true;
Layout();
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment