更新:
现在,在我写下这个答案将近一年后,我强烈建议使用ReactiveUI CollectionView/TableView绑定功能
mentioned by Paul Betts
。现在它的状态要成熟得多。
解决方案比预期的要困难一些。多亏了RX,在UICollectionViewDataSourceFlatReadOnly中可以很容易地解决对单个项目的插入或删除速率的限制问题。下一步涉及在UIDataBoundCollectionView中将这些更改批处理在一起。PerformBatchUpdate在这里没有帮助,但对所有插入的IndexPaths发出一个InsertItems调用确实解决了问题。
由于UICollectionView验证其内部一致性的方式(即在每个InsertItem或DeleteItems之后调用GetItemsCount等),我不得不将ItemCount管理移交给UIDataBoundCollectionView(这很难接受,但别无选择)。
顺便说一句,表现非常出色。
以下是感兴趣的人的最新消息来源:
ICollectionViewDataSource(集合视图数据源)
public interface ICollectionViewDataSource
{
/// <summary>
/// Gets the bound item at the specified index
/// </summary>
/// <param name="indexPath">The index path.</param>
/// <returns></returns>
object GetItemAt(NSIndexPath indexPath);
/// <summary>
/// Gets the actual item count.
/// </summary>
/// <value>The item count.</value>
int ActualItemCount { get; }
/// <summary>
/// Gets or sets the item count reported to UIKit
/// </summary>
/// <value>The item count.</value>
int ItemCount { get; set; }
/// <summary>
/// Observable providing change monitoring
/// </summary>
/// <value>The collection changed observable.</value>
IObservable<NotifyCollectionChangedEventArgs[]> CollectionChangedObservable { get; }
}
UI数据边界集合视图
[Register("UIDataBoundCollectionView")]
public class UIDataBoundCollectionView : UICollectionView,
IEnableLogger
{
public UIDataBoundCollectionView (NSObjectFlag t) : base(t)
{
}
public UIDataBoundCollectionView (IntPtr handle) : base(handle)
{
}
public UIDataBoundCollectionView (RectangleF frame, UICollectionViewLayout layout) : base(frame, layout)
{
}
public UIDataBoundCollectionView (NSCoder coder) : base(coder)
{
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if(collectionChangedSubscription != null)
{
collectionChangedSubscription.Dispose();
collectionChangedSubscription = null;
}
}
IDisposable collectionChangedSubscription;
public override NSObject WeakDataSource
{
get
{
return base.WeakDataSource;
}
set
{
if(collectionChangedSubscription != null)
{
collectionChangedSubscription.Dispose();
collectionChangedSubscription = null;
}
base.WeakDataSource = value;
collectionChangedSubscription = ICVS.CollectionChangedObservable
.Subscribe(OnDataSourceCollectionChanged);
}
}
ICollectionViewDataSource ICVS
{
get { return (ICollectionViewDataSource) WeakDataSource; }
}
void OnDataSourceCollectionChanged(NotifyCollectionChangedEventArgs[] changes)
{
List<NSIndexPath> indexPaths = new List<NSIndexPath>();
int index = 0;
for(;index<changes.Length;index++)
{
var e = changes[index];
switch(e.Action)
{
case NotifyCollectionChangedAction.Add:
indexPaths.AddRange(IndexPathHelper.FromRange(e.NewStartingIndex, e.NewItems.Count));
ICVS.ItemCount++;
// attempt to batch subsequent changes of the same type
if(index < changes.Length - 1)
{
for(int i=index + 1; i<changes.Length; i++)
{
if(changes[i].Action == e.Action)
{
indexPaths.AddRange(IndexPathHelper.FromRange(changes[i].NewStartingIndex, changes[i].NewItems.Count));
index++;
ICVS.ItemCount++;
}
}
}
InsertItems(indexPaths.ToArray());
indexPaths.Clear();
break;
case NotifyCollectionChangedAction.Remove:
indexPaths.AddRange(IndexPathHelper.FromRange(e.OldStartingIndex, e.OldItems.Count));
ICVS.ItemCount--;
// attempt to batch subsequent changes of the same type
if(index < changes.Length - 1)
{
for(int i=index + 1; i<changes.Length; i++)
{
if(changes[i].Action == e.Action)
{
indexPaths.AddRange(IndexPathHelper.FromRange(changes[i].OldStartingIndex, changes[i].OldItems.Count));
index++;
ICVS.ItemCount--;
}
}
}
DeleteItems(indexPaths.ToArray());
indexPaths.Clear();
break;
case NotifyCollectionChangedAction.Replace:
case NotifyCollectionChangedAction.Move:
PerformBatchUpdates(() =>
{
for(int i=0; i<e.OldItems.Count; i++)
MoveItem(NSIndexPath.FromItemSection(e.OldStartingIndex + i, 0), NSIndexPath.FromItemSection(e.NewStartingIndex + i, 0));
}, null);
break;
case NotifyCollectionChangedAction.Reset:
ICVS.ItemCount = ICVS.ActualItemCount;
ReloadData();
break;
}
}
}
}
UI集合视图DataSource平面只读
public class UICollectionViewDataSourceFlatReadOnly : UICollectionViewDataSource,
ICollectionViewDataSource
{
/// <summary>
/// Initializes a new instance of the <see cref="UICollectionViewDataSourceFlat"/> class.
/// </summary>
/// <param name="table">The table.</param>
/// <param name="items">The items.</param>
/// <param name="cellProvider">The cell provider</param>
public UICollectionViewDataSourceFlatReadOnly(IReadOnlyList<object> items, ICollectionViewCellProvider cellProvider)
{
this.items = items;
this.cellProvider = cellProvider;
// wire up proxying collection changes if supported by source
var ncc = items as INotifyCollectionChanged;
if(ncc != null)
{
collectionChangedObservable = Observable.FromEventPattern<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
h => ncc.CollectionChanged += h, h => ncc.CollectionChanged -= h)
.SubscribeOn(TaskPoolScheduler.Default)
.Select(x => x.EventArgs)
.Buffer(TimeSpan.FromMilliseconds(100), 20)
.Where(x => x.Count > 0)
.Select(x => x.ToArray())
.ObserveOn(RxApp.MainThreadScheduler)
.StartWith(new[] { reset}); // ensure initial update
}
else
collectionChangedObservable = Observable.Return(reset);
}
#region Properties
private IReadOnlyList<object> items;
private readonly ICollectionViewCellProvider cellProvider;
IObservable<NotifyCollectionChangedEventArgs[]> collectionChangedObservable;
static readonly NotifyCollectionChangedEventArgs[] reset = new[] { new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset) };
#endregion
#region Overrides of UICollectionViewDataSource
public override int NumberOfSections(UICollectionView collectionView)
{
return 1;
}
public override int GetItemsCount(UICollectionView collectionView, int section)
{
return ItemCount;
}
/// <summary>
/// Gets the cell.
/// </summary>
/// <param name="tableView">The table view.</param>
/// <param name="indexPath">The index path.</param>
/// <returns></returns>
public override UICollectionViewCell GetCell(UICollectionView collectionView, NSIndexPath indexPath)
{
// reuse or create new cell
var cell = (UICollectionViewCell) collectionView.DequeueReusableCell(cellProvider.Identifier, indexPath);
// get the associated collection item
var item = GetItemAt(indexPath);
// update the cell
if(item != null)
cellProvider.UpdateCell(cell, item, collectionView.GetIndexPathsForSelectedItems().Contains(indexPath));
// done
return cell;
}
#endregion
#region Implementation of ICollectionViewDataSource
/// <summary>
/// Gets the item at.
/// </summary>
/// <param name="indexPath">The index path.</param>
/// <returns></returns>
public object GetItemAt(NSIndexPath indexPath)
{
return items[indexPath.Item];
}
public int ActualItemCount
{
get
{
return items.Count;
}
}
public int ItemCount { get; set; }
public IObservable<NotifyCollectionChangedEventArgs[]> CollectionChangedObservable
{
get
{
return collectionChangedObservable;
}
}
#endregion
}