Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
Forked from prashantvc/TableSource.cs
Created September 30, 2013 11:59
Show Gist options
  • Save dannycabrera/6762755 to your computer and use it in GitHub Desktop.
Save dannycabrera/6762755 to your computer and use it in GitHub Desktop.
using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using System.Threading.Tasks;
namespace BasicTable {
public class TableSource : UITableViewSource {
protected string[] tableItems;
protected string cellIdentifier = "TableCell";
public TableSource (string[] items)
{
tableItems = items;
}
/// <summary>
/// Called by the TableView to determine how many cells to create for that particular section.
/// </summary>
public override int RowsInSection (UITableView tableview, int section)
{
return tableItems.Length;
}
/// <summary>
/// Called when a row is touched
/// </summary>
public override async void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
string sResponse = "";
var result = await ShowMessageAsync ("Continue?", "Row Selected", new string[] {"Yes", "No"});
if (result == "Yes") {
sResponse = "Did Continue";
} else {
sResponse = "Did NOT Continue";
}
new UIAlertView(sResponse, tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow (indexPath, true);
}
/// <summary>
/// Called by the TableView to get the actual UITableViewCell to render for the particular row
/// </summary>
public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
{
// request a recycled cell to save memory
UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);
// if there are no cells to reuse, create a new one
if (cell == null)
cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
cell.TextLabel.Text = tableItems[indexPath.Row];
return cell;
}
public Task<string> ShowMessageAsync (string sMessage, string sTitle, params string[] asButtons)
{
var view = new UIAlertView (sTitle, sMessage, null, null, asButtons);
var tsc = new TaskCompletionSource<string> ();
view.Show ();
view.Clicked += (sender, e) => {
tsc.SetResult (view.ButtonTitle (e.ButtonIndex)); //Set result end the await
};
return tsc.Task;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment