Advertisement
Magicbjorn

ContactOverViewDataSource

Feb 17th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.55 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Collections.Generic;
  4. using MonoTouch.Foundation;
  5. using MonoTouch.UIKit;
  6. using MonoTouch.ObjCRuntime;
  7. using System.Linq;
  8.  
  9. namespace Planning
  10. {
  11.     public class ContactOverviewDataSource : UITableViewSource
  12.     {
  13.         private List<Contact> contacts;
  14.         private Dictionary<string, List<Contact>> indexedTableItems;
  15.         private string[] keys;
  16.  
  17.         public ContactOverviewDataSource (List<Contact> contacts)
  18.         {
  19.             this.contacts = contacts;
  20.  
  21.             char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray ();
  22.             keys = new string[letters.Length];
  23.  
  24.             for (int i = 0; i < letters.Length; i++)
  25.             {
  26.                 keys [i] = letters [i].ToString ();
  27.             }
  28.  
  29.             indexedTableItems = new Dictionary<string, List<Contact>> ();
  30.  
  31.             foreach (var key in keys)
  32.             {
  33.                 List<Contact> temp = new List<Contact> ();
  34.  
  35.                 foreach (var contact in contacts)
  36.                 {
  37.                     if (contact.LastName.ToLower()[0].ToString() == key.ToLower ())
  38.                     {
  39.                         temp.Add (contact);
  40.                     }
  41.                 }
  42.                    
  43.                 indexedTableItems.Add (key, temp);
  44.             }
  45.         }
  46.  
  47.         public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
  48.         {
  49.             if (AppDelegate.IsPhone)
  50.             {
  51.                 return 35;
  52.             }
  53.  
  54.             else
  55.             {
  56.                 return 55;
  57.             }
  58.         }
  59.  
  60.         public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
  61.         {
  62.             if (OnRowSelected != null)
  63.             {
  64.                 OnRowSelected (this, new RowSelectedEventArgs (tableView, indexPath));
  65.             }
  66.         }
  67.  
  68.         public override int NumberOfSections (UITableView tableView)
  69.         {
  70.             int length = keys.Length;
  71.             int xSections = 0;
  72.  
  73.             for (int i = 0; i < length; i++)
  74.             {
  75.                 if (RowsInSection(tableView, i) > 0)
  76.                 {
  77.                     xSections++;
  78.                 }
  79.             }
  80.  
  81.             return xSections;
  82.         }
  83.  
  84.         public override int RowsInSection (UITableView tableview, int section)
  85.         {
  86.             return indexedTableItems [keys [section]].Count;
  87.         }
  88.  
  89.         public override string[] SectionIndexTitles (UITableView tableView)
  90.         {
  91.             return keys;
  92.         }
  93.  
  94.         public override string TitleForHeader (UITableView tableView, int section)
  95.         {
  96.             if (RowsInSection (tableView, section) > 0)
  97.             {
  98.                 return keys [section];
  99.             }
  100.  
  101.             else
  102.             {
  103.                 return null;
  104.             }
  105.         }
  106.  
  107.         public override UIView GetViewForHeader (UITableView tableView, int section)
  108.         {
  109.             if (RowsInSection (tableView, section) > 0)
  110.             {
  111.                 var view = new UIView (new RectangleF (0, 0, tableView.Bounds.Width, 37));
  112.  
  113.                 var label = new UILabel (new RectangleF (25, 5, 120, 50))
  114.                 {
  115.                     Text = TitleForHeader (tableView, section),
  116.                     TextAlignment = UITextAlignment.Left,
  117.                     TextColor = UIColor.White,
  118.                     Font = UIFont.BoldSystemFontOfSize (UIFont.LabelFontSize)
  119.                 };
  120.  
  121.                 label.SizeToFit ();
  122.                 view.BackgroundColor = UIColor.DarkGray;
  123.                 view.AddSubview (label);
  124.  
  125.                 return view;
  126.             }
  127.  
  128.             else
  129.             {
  130.                 return null;
  131.             }
  132.         }
  133.  
  134.         public override float GetHeightForHeader (UITableView tableView, int section)
  135.         {
  136.             if (RowsInSection (tableView, section) > 0)
  137.             {
  138.                 return 30;
  139.             }
  140.  
  141.             else
  142.             {
  143.                 return 0;
  144.             }
  145.         }
  146.  
  147.         public class RowSelectedEventArgs : EventArgs
  148.         {
  149.             public UITableView tableView { get; set; }
  150.  
  151.             public NSIndexPath indexPath { get; set; }
  152.  
  153.             public RowSelectedEventArgs (UITableView tableView, NSIndexPath indexPath) : base ()
  154.             {
  155.                 this.tableView = tableView;
  156.                 this.indexPath = indexPath;
  157.             }
  158.         }
  159.  
  160.         public event EventHandler<RowSelectedEventArgs> OnRowSelected;
  161.  
  162.         public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
  163.         {
  164.             Contact contact = null;
  165.  
  166.             List<Contact> temp = new List<Contact> ();
  167.  
  168.             foreach (var c in contacts)
  169.             {
  170.                 if (c.LastName.ToLower()[0].ToString() == keys [indexPath.Section].ToLower())
  171.                 {
  172.                     temp.Add (c);
  173.                 }
  174.             }
  175.  
  176.             contact = temp [indexPath.Row];
  177.  
  178.             UITableViewCell cell = new UITableViewCell (new RectangleF (0, 0, AppDelegate.ScreenWidth, 90));
  179.             UIView bg = new UIView (cell.Bounds);
  180.             bg.BackgroundColor = AppDelegate.Theme;
  181.  
  182.             cell.SelectionStyle = UITableViewCellSelectionStyle.Default;
  183.             cell.SelectedBackgroundView = bg;
  184.  
  185.             string info = "(" + contact.ContactID + ") " + contact.ContactName;
  186.  
  187.             UILabel lblInfo = new UILabel (new RectangleF (5, 7, AppDelegate.ScreenWidth - cell.Frame.X - 5, cell.Frame.Height));
  188.             lblInfo.TextAlignment = UITextAlignment.Left;
  189.             lblInfo.Text = info;
  190.             lblInfo.Font = AppDelegate.TextFont;
  191.             lblInfo.Lines = 0;
  192.             lblInfo.LineBreakMode = UILineBreakMode.WordWrap;
  193.             lblInfo.SizeToFit ();
  194.        
  195.             cell.AddSubview (lblInfo);
  196.             return cell;
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement