public void SortData(NSSortDescriptor[] descriptors) { NSIndexSet selections = tableView.SelectedRows; // Get a list of people to be removed List <Person> selectedPersons = new List <Person>(); for (int i = 0; i < selections.Count; i++) { int index = (int)selections.ElementAt(i); Person p = Employees[index]; selectedPersons.Add(p); } if (descriptors.Count <NSSortDescriptor>() > 0) { NSSortDescriptor descriptor = descriptors[0]; if (descriptor.Key == "name") { if (descriptor.Ascending) { _employees.Sort((emp1, emp2) => emp1.Name.ToLower().CompareTo(emp2.Name.ToLower())); } else { _employees.Sort((emp1, emp2) => emp2.Name.ToLower().CompareTo(emp1.Name.ToLower())); } } else if (descriptor.Key == "expectedRaise") { if (descriptor.Ascending) { _employees.Sort((emp1, emp2) => emp1.ExpectedRaise.CompareTo(emp2.ExpectedRaise)); } else { _employees.Sort((emp1, emp2) => emp2.ExpectedRaise.CompareTo(emp1.ExpectedRaise)); } } } tableView.DeselectAll(this); foreach (Person p in selectedPersons) { tableView.SelectRow(Employees.IndexOf(p), true); } }
void GetResponse(NSAlert alert, nint response) { if (response <= 1) { switch (response) { case -1: NSIndexSet rows = tableView.SelectedRows; for (int i = 0; i < rows.Count; i++) { Person p = Employees[(int)rows.ElementAt(i)]; p.SetValueForKey(new NSNumber(0.0f), new NSString("expectedRaise")); } break; case 0: // OK deleteSelectedEmployees(this); break; default: // Cancel break; } } }
partial void deleteSelectedEmployees(Foundation.NSObject sender) { if (!StopEditing()) { return; } // Which row(s) are selected? NSIndexSet rows = tableView.SelectedRows; // Get a list of people to be removed List <Person> removedPersons = new List <Person>(); NSUndoManager undo = this.UndoManager; for (int i = 0; i < rows.Count; i++) { int index = (int)rows.ElementAt(i); Person p = Employees[index]; removedPersons.Add(p); } // remove each Person and add to the undo stack foreach (Person p in removedPersons) { Console.WriteLine("Removing {0} from {1}", p, Employees); // Add the inverse of this operation to the undo stack int index = Employees.IndexOf(p); NSArray args = NSArray.FromObjects(new object[] { p, new NSNumber(index) }); undo.RegisterUndoWithTarget(this, new Selector("undoRemove:"), args); undo.SetActionname(NSBundle.MainBundle.LocalizedString("REMOVE_PERSON", null)); StopObservingPerson(p); Employees.Remove(p); } tableView.ReloadData(); tableView.DeselectAll(this); }