1

I've got a UICollectionView inheriting from MvxViewController. It contains cells inheriting from MvxCollectionViewCell. The data is bound to the source through CreateBindingSet() in UICollectionView and I use DelayBind() to bind the data to the Cell when it's instantiated.

I've subclassed MvxCollectionViewSource and overridden GetOrCreateCellFor() method. Now whenever I load the data, the dequed cells retain old data.

I've considered 2 options:

  1. Create a new Cell in GetOrCreateCellFor() method. It fails because I need to pass an IntPtr in the constructor and I can't create no-arg ctor due to the contract with MvxCollectionViewCell.

  2. Update bindings when the Cell is displayed during WillDisplayCell(). This doesn't work and the old data is shown.

Any way out of this?

Edit: Added code

-- ViewWillAppear of MyView : MvxViewController

public class MyView : MvxViewController{
    // other stuff
public override void ViewWillAppear (bool animated)
    {
        var sourceOne = new MyCollectionViewSource (MyCollectionView, new NSString ("MyCollectionViewCell"));
        MyCollectionView.Source = sourceOne;
        var bindings = this.CreateBindingSet<MyView, MyViewModel> ();

        bindings.Bind (sourceOne)
                .To (vm => vm.ItemList);

        bindings.Apply ();
        MyCollectionView.ReloadData ();
    }
}

-- MyCollectionViewSource : MvxCollectionViewSource

public MyCollectionViewSource : MvxCollectionViewSource{
protected override UICollectionViewCell GetOrCreateCellFor (UICollectionView collectionView, NSIndexPath indexPath, object item)
        {
            return collectionView.DequeueReusableCell (new NSString ("MyCollectionViewCell"), indexPath) as MyCollectionViewCell;
        }
public override void WillDisplayCell (UICollectionView collectionView, UICollectionViewCell cell, NSIndexPath indexPath)
        {
            var cellz = cell as MyCollectionViewCell;
            cellz.ClearAllBindings ();
            cellz.BindUrl ();
    }
}

-- The Cell

public partial class MyCollectionViewCell : MvxCollectionViewCell {    
        public MyCollectionViewCell (IntPtr handle) : base (handle)
        {
            BindUrl();
        }
        public void BindUrl()
        {
            this.ClearAllBindings ();
            this.DelayBind(() => {
                this.CreateBinding(MyWebView)
                    .For(webView => webView.MyUrl)
                    .To<ListItemViewModel>(vm => vm.UrlEntryPoint)
                    .Apply();
          });
}
8
  • 1
    IMO this question really needs code included. Overriding GetOrCreateCellFor does not normally stop binding working - I've done this plenty of times. However, the devil is in the detail. How are you doing what you are doing? How can readers of this question guess that?
    – Stuart
    Nov 28, 2014 at 9:52
  • @Stuart I've added the missing code. Nov 28, 2014 at 11:59
  • Why are you calling cellz.ClearAllBindings (); - that removes all Data Bindings
    – Stuart
    Nov 28, 2014 at 12:21
  • I remove all the previous bindings of the Cell and recreate it. I also tried with cellz.ClearAllBindings () commented, and it's still the same :-( Nov 28, 2014 at 12:56
  • Maybe start from something that works - e.g. slodge.blogspot.co.uk/2013/05/… - and see if you can spot what is causing your issues... Currently it feels like you have several extra bits of code that neither of us is sure why they've been added.
    – Stuart
    Nov 28, 2014 at 13:04

1 Answer 1

0

Ok, so I've solved this issue by mixing couple of strategies.

  1. Created a SelectedItemNo property in the DataSource

  2. In the Delegate method DecelerationEnded I calculated the current index of the cell and change the CollectionView.SelectedItemNo property to match the current visible cell.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.