Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating MvxTableViewCell without nib

I want to make MvxTableViewCell without nib file, but I search in stackoverflow, github and google, I couldn't good sample to make MvxTableViewCell manually.

I tried such code,

namespace KittenView.Touch
{
    public partial class KittenCell : MvxTableViewCell
    {
        public static readonly NSString Key = new NSString ("KittenCell");

        private readonly MvxImageViewLoader _imageViewLoader;
        MonoTouch.UIKit.UIImageView MainImage { get; set; }
        MonoTouch.UIKit.UILabel NameLabel { get; set; }
        MonoTouch.UIKit.UILabel PriceLabel { get; set; }

        public KittenCell () : base ()
        {
            this.Frame = new RectangleF(0f,0f,100f,120f);
            MainImage = new UIImageView (new RectangleF(0f,0f,100f,100f));
            NameLabel = new UILabel (new RectangleF (0f, 80f, 100f, 20f));
            PriceLabel = new UILabel (new RectangleF (0f, 100f, 100f, 20f));
            Add (MainImage);
            Add (NameLabel);
            Add (PriceLabel);

            _imageViewLoader = new MvxImageViewLoader(() => this.MainImage);

            this.DelayBind (() => {
                var set = this.CreateBindingSet<KittenCell, Kitten>();
                set.Bind(NameLabel).To (kitten => kitten.Name);
                set.Bind(PriceLabel).To(kitten => kitten.Price);
                set.Bind(_imageViewLoader).To (kitten => kitten.ImageUrl);
                set.Apply ();
            });

            this.Transform = CGAffineTransform.MakeRotation ((float)Math.PI / 2.0f);
        }

        public static KittenCell Create ()
        {
            return new KittenCell ();
        }
    }
}  

But this code occurs Exception in execution phase, said

SetValue:forUndefinedKey:]this calss in not key value coding-compliant for key MainImage.

at AppDelegate.cs's window.MakeKeyAndVisible().

How do I use MvxTableViewCell without nib?

Regards,

Ko-hei

like image 469
HistoricalMapNation Avatar asked Aug 20 '26 12:08

HistoricalMapNation


1 Answers

The easiest way to do this is to use RegisterClassForCell to register your cell with the table view and DequeueReusableCell to then later create or reuse cells.

In order to allow UIKit to create instances of this class using this method inside the dequeue calls, then I think you need to add;

  • a [Register("KittenCell")] attribute in front of the class - this attribute is used by MonoTouch to tell the ObjectiveC runtime about this managed class - it allows instances of this managed class to be created from ObjectiveC,

  • a constructor which takes an (IntPtr handle) parameter which it passes down to the base(handle) constructor. This constructor is used to allow the managed C# object to be created alongside the underlying unmanaged UIKit object.

If you add those 2 items, I believe your celll will get created. Further, if you switch to the simpler RegisterClassForCell API, then I think you can remove the parameterless constructor and the static Create method.

like image 56
Stuart Avatar answered Aug 22 '26 18:08

Stuart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!