0

I created custom MvxTableViewCell, placed all design layouts in .xib file:

public partial class FxTransactionCell: MvxTableViewCell
{
    public static readonly NSString Key = new NSString("FxTransactionCell");
    public static readonly UINib Nib = UINib.FromName(Key, NSBundle.MainBundle);

    static FxTransactionCell()
    {
    }

    protected FxTransactionCell(IntPtr handle): base(handle)
    {
        // Note: this .ctor should not contain any initialization logic.
    }
}

All samples I've seen placed initialization/binding logic into (IntPtr) constructor, but note that comment placed there by VS. I think this constructor can not hold any init logic, because my custom UI elements not yet created and all my UILabels, UIButtons (which layouts in .xib file) are null inside this constructor. So, where I should place my init/bindings logic then?

zzheads
  • 1,368
  • 5
  • 28
  • 57

1 Answers1

0

In your public constructor you should put your init/binding logic. Pay attention that you have to use DelayBind(...) to do binding inside an MvxTableViewCell

public FxTransactionCell()
{
    // Init views if you need to

    // Create the bindings
    this.DelayBind(this.CreateBindings);
}

public FxTransactionCell(IntPtr handle) : base(handle)
{
    // Some people repeat the same logic here just in case the above ctor does not get called, ignoring the note from VS but I don't think it's necessary.
}

private void CreateBindings()
{
    var set = this.CreateBindingSet<FxTransactionCell, FxTransactionItemViewModel>();
    set.Bind(this.MyLabel).To(vm => vm.MyStringProperty);
    set.Apply();
}

HIH

fmaccaroni
  • 3,846
  • 1
  • 20
  • 35
  • Doesn't work, that c'tor not called by TableView.DequeueReusableCell method – zzheads Nov 10 '18 at 07:34
  • And if you do the logic in the intptr ctor, does it get called? – fmaccaroni Nov 10 '18 at 14:30
  • Yep, but it throws NullException, because all subviews inside IntPtr ctor are null – zzheads Nov 10 '18 at 14:33
  • 1
    I solve problem by calling custom method Config(FxTransactionCellViewModel) from GetOrCreateCell method of my data source, but is there other way to do it, bind all in some constructor? Or It is possible ONLY if I create all cell subviews programmatically? – zzheads Nov 10 '18 at 14:38
  • If you create the cells programmatically I'm sure it is possible because is the way I do it but from Nib I don't really know if it is possible. I'll see if I can find anything about it – fmaccaroni Nov 10 '18 at 15:43
  • 1
    Thanks a lot, dude! – zzheads Nov 10 '18 at 16:12
  • For cells created via nib, I use AwakeFromNib override in the UITableViewCell class – pnavk Nov 20 '18 at 23:07
  • 1
    You need to use the `protected` constructor. `protected FxTransactionCell(IntPtr handle) : base(handle)` and add the `DelayBinding()` in there. Also as suggested it should inherit from `MvxTableViewCell`. This is the correct answer so if it does not work then there is a problem somewhere else in your code. – c.lamont.dev Nov 21 '18 at 07:37