public void RepositionRowForComparable(int comparableId) { var comparable = (from c in Comparables where c.Id == comparableId select c).FirstOrDefault(); if (comparable == null) { return; } // refresh the comparable object var index = Comparables.IndexOf(comparable); var refreshedComparable = DataService.GetComparable(comparableId); Comparables[index] = refreshedComparable; // reorder the list again Comparables = Comparables.OrderBy(c => c.GetPricePerBaseUnit(Comparison.UnitId)).ToList(); // get the new index of the comparable var newIndex = Comparables.IndexOf(refreshedComparable); if (newIndex == index) { var indexPaths = new NSIndexPath[] { NSIndexPath.FromRowSection(index, 0) }; ReloadRows(indexPaths, UITableViewRowAnimation.None); SelectRow(indexPaths[0], false, UITableViewScrollPosition.None); } else { BeginUpdates(); InsertRows(new NSIndexPath[] { NSIndexPath.FromRowSection(newIndex, 0) }, UITableViewRowAnimation.Fade); DeleteRows(new NSIndexPath[] { NSIndexPath.FromRowSection(index, 0) }, UITableViewRowAnimation.Fade); EndUpdates(); if (newIndex == 0) { UpdateCheapestComparable(refreshedComparable); } } }
public void AddComparable(ComparableModel comparable) { if (Comparables.Count == 0) { Comparables.Add(comparable); UpdateCheapestComparable(comparable); ReloadRows(new NSIndexPath[] { NSIndexPath.FromRowSection(0, 0) }, UITableViewRowAnimation.Fade); } else { Comparables.Add(comparable); Comparables = Comparables.OrderBy(c => c.GetPricePerBaseUnit(Comparison.UnitId)).ToList(); var addedAt = Comparables.IndexOf(comparable); if (addedAt == 0) { UpdateCheapestComparable(comparable); } BeginUpdates(); InsertRows(new NSIndexPath[] { NSIndexPath.FromRowSection(addedAt, 0) }, UITableViewRowAnimation.Fade); EndUpdates(); } SetScrollAndSelection(); }