public void PopulateTable(){

			List<CustomCellGroup> cellGroups = new List<CustomCellGroup> ();
			tableCellGroup = new CustomCellGroup ();
			cellGroups.Add (tableCellGroup);

			for (int x = 0; x <= 8; x++) {
				ContactKeysCell chatCell = ContactKeysCell.Create ();
				chatCell.SetHeader("John Doe (" + x + ")");
				chatCell.SetSubHeader("XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX");
				tableCellGroup.Cells.Add (chatCell);
			}

			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;

		}
		public void PopulateTable(){

			List<CustomCellGroup> cellGroups = new List<CustomCellGroup> ();
			tableCellGroup = new CustomCellGroup ();
			cellGroups.Add (tableCellGroup);

			SettingsCell cell;
			SettingsHeader hCell;

			hCell = SettingsHeader.Create ();
			hCell.SetHeader("REGISTRATION");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Turn this off to unregister your account";
			tableCellGroup.Cells.Add (cell);

			hCell = SettingsHeader.Create ();
			hCell.SetHeader("NOTIFICATIONS");
			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Notifications");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Display message notifications in the status bar";
			tableCellGroup.Cells.Add (cell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("In-thread notifications");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Display message notifications in status bar";
			tableCellGroup.Cells.Add (cell);

			hCell = SettingsHeader.Create ();
			hCell.SetHeader("INPUT SETTINGS");
			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Enter sends");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Pressing the enter key will send text messages";
			tableCellGroup.Cells.Add (cell);

//			hCell = SettingsHeader.Create ();
//			hCell.SetHeader("APPEARANCE");
//			tableCellGroup.Cells.Add (hCell);
//
//			hCell = SettingsHeader.Create ();
//			hCell.SetSubheader("Theme");
//			tableCellGroup.Cells.Add (hCell);
//
//			hCell = SettingsHeader.Create ();
//			hCell.SetSubheader("Language");
//			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetHeader("STORAGE");
			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Delete old messages");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Automatically delete older messages once a conversation thread reaches a certain length";
			cell.sectionButton.ValueChanged += delegate {

			};
			tableCellGroup.Cells.Add (cell);

//			hCell = SettingsHeader.Create ();
//			hCell.SetSubheader("Conversation limit length");
//			tableCellGroup.Cells.Add (hCell);
//
//			hCell = SettingsHeader.Create ();
//			hCell.SetSubheader("Trim all threads now");
//			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetHeader("ADVANCED");
			tableCellGroup.Cells.Add (hCell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Complete key exchanges");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Automatically complete key exchanges for new session or for existing sessions with the same identity key";
			tableCellGroup.Cells.Add (cell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Screen security");
			tableCellGroup.Cells.Add (hCell);

			cell = SettingsCell.Create ();
			cell.sectionDetail.Text = "Disable screen security to allow screen shots";
			tableCellGroup.Cells.Add (cell);

			hCell = SettingsHeader.Create ();
			hCell.SetSubheader("Refresh Push Directory");
			tableCellGroup.Cells.Add (hCell);

			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;

		}
		public void PopulateTable(string afilter)
		{
			this.filter = afilter;
			cellGroups = new List<CustomCellGroup>();
			AddContactsData(afilter);
			//AddTestData();
			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;
			AddTableRefresh();
		}
		public void PopulateTableWithExistingContactsData()
		{
			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;
		}
		private void ProcessSearchOnSecurecomContacts(string searchText)
		{
			List<CustomCellGroup> mCellGroups = new List<CustomCellGroup>();
			List<PushContact> pc;
			CustomCellGroup mTableCellGroup = new CustomCellGroup { Name = "No Results" };
			using (var conn = new SQLite.SQLiteConnection(AppDelegate._dbPath)) {
				pc = conn.Query<PushContact>("select * from PushContact");
			}
			foreach (PushContact _c in pc) {
				if (String.IsNullOrEmpty(_c.Name))
					continue;
				if (_c.Name.ToLower().Contains(searchText) || _c.Number.Contains(searchText)) {
					mTableCellGroup.Name = "Search Results";
					ContactListCell cell = ContactListCell.Create();
					cell.SetName(_c.Name);
					if (_c.Number.Contains("@")) {
						cell.SetEmail(_c.Number);
					} else {
						cell.SetPhone(_c.Number);
					}
					mTableCellGroup.Cells.Add(cell);
				}
			}
			mCellGroups.Add(mTableCellGroup);
			source = new CustomCellTableSource(mCellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;
		}
		private void ProcessSearchOnAllContacts(string searchText)
		{
			List<CustomCellGroup> mCellGroups = new List<CustomCellGroup>();
			CustomCellGroup mTableCellGroup = new CustomCellGroup();
			AddressBook book = new AddressBook();
			foreach (Contact contact in book.OrderBy(c => c.DisplayName)) {
				if (String.IsNullOrEmpty(contact.DisplayName))
					continue;
				bool found = false;
				//Decide contact group based on the first character
				String group = contact.DisplayName.Substring(0, 1).ToUpper();
				if (contact.DisplayName.ToLower().Contains(searchText)) {
					found = true;
				} else if (contact.Phones.Any()) {
					foreach (Phone p in contact.Phones) {
						String temp = p.Number
							.Replace("(", string.Empty)
							.Replace(")", string.Empty)
							.Replace("-", string.Empty)
							.Replace(" ", string.Empty);
						found |= temp.Contains(searchText);
					}
				} else if (contact.Emails.Any()) {
					foreach (Email e in contact.Emails) {
						found |= e.Address.ToLower().Contains(searchText);
					}
				}
				if (!found)
					continue;
				ContactListCell cell = ContactListCell.Create();
				cell.SetName(contact.DisplayName);
				foreach (Phone p in contact.Phones) {
					cell.SetPhone(p.Number);
					break;
				}
				foreach (Email e in contact.Emails) {
					cell.SetEmail(e.Address);
					break;
				}
				mTableCellGroup.Cells.Add(cell);
			}
			mTableCellGroup.Name = mTableCellGroup.Cells.Count == 0 ? "No Results" : "Search Results";
			mCellGroups.Add(mTableCellGroup);
			source = new CustomCellTableSource(mCellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;
		}
		private void LoadTable(ChatBubbleCell _cell)
		{

			List<CustomCellGroup> cellGroups = new List<CustomCellGroup>();
			tableCellGroup = new CustomCellGroup();
			cellGroups.Add(tableCellGroup);

			ChatBubbleCell cell;
			for (int x = 0; x < messages.Count; x++) {
				cell = new ChatBubbleCell(isLeft[x], false, isdelivered[x]);
				cell.Update(messages[x], timeStamps[x]);
				cell.SetMessageID(message_ids[x]);
				if (!isdelivered[x])
					cell.SetAsUndelivered();
				tableCellGroup.Cells.Add(cell);
			}

			if (_cell != null) {
				tableCellGroup.Cells.Add(_cell);
			} 

			SetTableSize();


//			//Pending
//			cell = new ChatBubbleCell(false);
//			cell.Update ("This is a pending cell", "8:00 PM");
//			cell.SetAsPending ();
//			tableCellGroup.Cells.Add (cell);
//
//			//Undelivered
//			cell = new ChatBubbleCell(true);
//			cell.Update ("This is a undelivered cell", "8:05 PM");
//			cell.SetAsUndelivered ();
//			tableCellGroup.Cells.Add (cell);

			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			source.DeleteAction = DeleteSelected;
			source.DeleteTitle = "Delete";
			table.Source = source;
		}
		public void PopulateTable(){
			List<CustomCellGroup> cellGroups = new List<CustomCellGroup> ();
			tableCellGroup = new CustomCellGroup ();
			cellGroups.Add (tableCellGroup);
			AddTestData ();
			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			table.Source = source;
		}
		public void PopulateAddContactTable(){
			List<CustomCellGroup> cellGroups = new List<CustomCellGroup> ();
			AddAcTestData (cellGroups);
			acSource = new CustomCellTableSource(cellGroups);
			acSource.RowSelectedAction = ContactRowSelected;
			addContactTable.Source = acSource;
		}
		public override void ViewDidLoad()
		{
			base.ViewDidLoad();
			this.appDelegate = (AppDelegate)UIApplication.SharedApplication.Delegate;

			appDelegate.chatView.setThreadID(0);

			ShowEditButton();

			this.composeButton.Clicked += (sender, e) => {
				ComposeAction();
			};
		
			this.editButton.Clicked += (sender, e) => {
				ShowDoneButton();
			};

			this.doneButton.Clicked += (sender, e) => {
				ShowEditButton();
			};

			this.keysButton.Clicked += (sender, e) => {
				this.appDelegate.GoToView(this.appDelegate.myIdKeyView);
			};


			this.markAllReadButton.Clicked += (sender, e) => {
				this.appDelegate.alert.showOkAlert("Mark All Read", "Marking all chat messages as read.");
			};

			settingsButton.Clicked += (sender, e) => {
				//this.appDelegate.GoToView(this.appDelegate.settingsView);
				SettingsAction();
			};

			search.CancelButtonClicked += (sender, e) => {
				search.Text = "";
				search.ResignFirstResponder();
				search.SetShowsCancelButton(false, true);
				PopulateTable();
			};

			search.TextChanged += async (object sender, UISearchBarTextChangedEventArgs e) => {
				search.SetShowsCancelButton(true, true);
				String searchText = e.SearchText.ToLower();
				if (e.SearchText.Equals("")) {
					PopulateTable();
					return;
				}
				List<CustomCellGroup> mCellGroups = new List<CustomCellGroup>();
				CustomCellGroup mTableCellGroup = new CustomCellGroup();
				int count = 0;
				foreach (PushChatThread thread in pct) {
					if (String.IsNullOrEmpty(thread.Number))
						continue;
					if (!thread.Number.ToLower().Contains(searchText) && !thread.DisplayName.ToLower().Contains(searchText))
						continue;
					
					ChatCell chatCell = ChatCell.Create();
					chatCell.SetHeader(thread.DisplayName + " (" + thread.Number + ")");
					chatCell.SetSubheading(thread.Snippet);
					chatCell.SetThreadID(thread.ID);
					chatCell.SetNumber(thread.Number);
					chatCell.SetAvatar(null);
					DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(thread.TimeStamp / 1000).ToLocalTime();
					chatCell.SetLabelTime(epoch.ToString("HH:mm"));
					chatCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
					mTableCellGroup.Cells.Insert(count, chatCell);
					count++;
				}
					
				mTableCellGroup.Name = (count == 0) ? "No Results" : "Search Results";
				mCellGroups.Add(mTableCellGroup);

				source = new CustomCellTableSource(mCellGroups);
				source.RowSelectedAction = RowSelected;
				source.DeleteAction = DeleteSelected;
				source.DeleteTitle = "Delete";
				table.Source = source;
				table.ReloadData();
			};

		}
		public void PopulateTable()
		{
			table.ReloadData();
			UIImage thumbnail = null;
			List<CustomCellGroup> cellGroups = new List<CustomCellGroup>();
			tableCellGroup = new CustomCellGroup();
			cellGroups.Add(tableCellGroup);
			PhoneNumberUtil phoneUtil = PhoneNumberUtil.GetInstance();
			AddressBook book = new AddressBook();
			var conn = new SQLite.SQLiteConnection(AppDelegate._dbPath);
			pct = conn.Query<PushChatThread>("SELECT * FROM PushChatThread ORDER BY TimeStamp DESC");
			conn.Close();
			int count = 0;

			foreach (PushChatThread thread in pct) {
				String display_name = thread.DisplayName;

				ChatCell chatCell = ChatCell.Create();
				chatCell.SetHeader(display_name + " (" + thread.Number + ")");
				chatCell.SetSubheading(thread.Snippet);
				chatCell.SetThreadID(thread.ID);
				chatCell.SetNumber(thread.Number);
				chatCell.SetAvatar(thumbnail);
				DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddSeconds(thread.TimeStamp / 1000).ToLocalTime();
				chatCell.SetLabelTime("" + epoch.ToString("HH:mm"));
				chatCell.Accessory = UITableViewCellAccessory.DisclosureIndicator;
					
				if (thread.Read != 0)
					chatCell.BackgroundColor = UIColor.Green;
				tableCellGroup.Cells.Insert(count, chatCell);
				count++;

			}
			source = new CustomCellTableSource(cellGroups);
			source.RowSelectedAction = RowSelected;
			source.DeleteAction = DeleteSelected;
			source.DeleteTitle = "Delete";
			table.Source = source;

			ShowEditButton();

		}