Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Collections.Generic;
- using MonoTouch.Foundation;
- using MonoTouch.UIKit;
- using MonoTouch.ObjCRuntime;
- using System.Linq;
- namespace Planning
- {
- public class ContactOverviewDataSource : UITableViewSource
- {
- private List<Contact> contacts;
- private Dictionary<string, List<Contact>> indexedTableItems;
- private string[] keys;
- public ContactOverviewDataSource (List<Contact> contacts)
- {
- this.contacts = contacts;
- char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray ();
- keys = new string[letters.Length];
- for (int i = 0; i < letters.Length; i++)
- {
- keys [i] = letters [i].ToString ();
- }
- indexedTableItems = new Dictionary<string, List<Contact>> ();
- foreach (var key in keys)
- {
- List<Contact> temp = new List<Contact> ();
- foreach (var contact in contacts)
- {
- if (contact.LastName.ToLower()[0].ToString() == key.ToLower ())
- {
- temp.Add (contact);
- }
- }
- indexedTableItems.Add (key, temp);
- }
- }
- public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
- {
- if (AppDelegate.IsPhone)
- {
- return 35;
- }
- else
- {
- return 55;
- }
- }
- public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
- {
- if (OnRowSelected != null)
- {
- OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
- }
- }
- public override int NumberOfSections (UITableView tableView)
- {
- int length = keys.Length;
- int xSections = 0;
- for (int i = 0; i < length; i++)
- {
- if (RowsInSection(tableView, i) > 0)
- {
- xSections++;
- }
- }
- return xSections;
- }
- public override int RowsInSection (UITableView tableview, int section)
- {
- return indexedTableItems [keys [section]].Count;
- }
- public override string[] SectionIndexTitles (UITableView tableView)
- {
- return keys;
- }
- public override string TitleForHeader (UITableView tableView, int section)
- {
- if (RowsInSection (tableView, section) > 0)
- {
- return keys [section];
- }
- else
- {
- return null;
- }
- }
- public override UIView GetViewForHeader (UITableView tableView, int section)
- {
- if (RowsInSection (tableView, section) > 0)
- {
- var view = new UIView (new RectangleF (0, 0, tableView.Bounds.Width, 37));
- var label = new UILabel (new RectangleF (25, 5, 120, 50))
- {
- Text = TitleForHeader (tableView, section),
- TextAlignment = UITextAlignment.Left,
- TextColor = UIColor.White,
- Font = UIFont.BoldSystemFontOfSize (UIFont.LabelFontSize)
- };
- label.SizeToFit ();
- view.BackgroundColor = UIColor.DarkGray;
- view.AddSubview (label);
- return view;
- }
- else
- {
- return null;
- }
- }
- public override float GetHeightForHeader (UITableView tableView, int section)
- {
- if (RowsInSection (tableView, section) > 0)
- {
- return 30;
- }
- else
- {
- return 0;
- }
- }
- public class RowSelectedEventArgs : EventArgs
- {
- public UITableView tableView { get; set; }
- public NSIndexPath indexPath { get; set; }
- public RowSelectedEventArgs (UITableView tableView, NSIndexPath indexPath) : base ()
- {
- this.tableView = tableView;
- this.indexPath = indexPath;
- }
- }
- public event EventHandler<RowSelectedEventArgs> OnRowSelected;
- public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
- {
- Contact contact = null;
- List<Contact> temp = new List<Contact> ();
- foreach (var c in contacts)
- {
- if (c.LastName.ToLower()[0].ToString() == keys [indexPath.Section].ToLower())
- {
- temp.Add (c);
- }
- }
- contact = temp [indexPath.Row];
- UITableViewCell cell = new UITableViewCell (new RectangleF (0, 0, AppDelegate.ScreenWidth, 90));
- UIView bg = new UIView (cell.Bounds);
- bg.BackgroundColor = AppDelegate.Theme;
- cell.SelectionStyle = UITableViewCellSelectionStyle.Default;
- cell.SelectedBackgroundView = bg;
- string info = "(" + contact.ContactID + ") " + contact.ContactName;
- UILabel lblInfo = new UILabel (new RectangleF (5, 7, AppDelegate.ScreenWidth - cell.Frame.X - 5, cell.Frame.Height));
- lblInfo.TextAlignment = UITextAlignment.Left;
- lblInfo.Text = info;
- lblInfo.Font = AppDelegate.TextFont;
- lblInfo.Lines = 0;
- lblInfo.LineBreakMode = UILineBreakMode.WordWrap;
- lblInfo.SizeToFit ();
- cell.AddSubview (lblInfo);
- return cell;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement