Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

ItemAppearing not called in custom ViewCell

$
0
0

I am using FastCell (custom ViewCell) in my ListView and it appears ItemAppearing is never called. Anyone know how to get it to work with custom view cells?

FastCell:
https://github.com/georgejecook/xamarinFastCell

Renderer for iOS:

using System;
using MyApp.Controls;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using Foundation;
using CoreGraphics;
using MyApp.iOS;

[assembly: ExportRenderer (typeof(FastCell), typeof(FastCellRenderer))]
namespace MyApp.iOS
{
    internal class NativeCell : UITableViewCell
    {

        UIView _view;
        object _originalBindingContext;

        FastCell _fastCell;

        public NativeCell (NSString cellId, FastCell fastCell) : base (UITableViewCellStyle.Default, cellId)
        {
            _fastCell = fastCell;
            _fastCell.PrepareCell ();
            _originalBindingContext = fastCell.BindingContext;
            var renderer = RendererFactory.GetRenderer (fastCell.View);
            _view = renderer.NativeView;
            ContentView.AddSubview (_view);
        }

        public void RecycleCell (FastCell newCell)
        {
            if (newCell == _fastCell) {
                _fastCell.BindingContext = _originalBindingContext;
            } else {
                _fastCell.BindingContext = newCell.BindingContext;
            }
        }

        CGSize _lastSize;

        public override void LayoutSubviews ()
        {
            base.LayoutSubviews ();
            //TODO update sizes of the xamarin view
            if (_lastSize.Equals (CGSize.Empty) || !_lastSize.Equals (Frame.Size)) {

                var layout = _fastCell.View as Layout<View>;
                if (layout != null) {
                    layout.Layout (Frame.ToRectangle ());
                    layout.ForceLayout ();
                    FixChildLayouts (layout);
                }
                _lastSize = Frame.Size;
            }

            _view.Frame = ContentView.Bounds;
        }

        void FixChildLayouts (Layout<View> layout)
        {
            foreach (var child in layout.Children) {
                if (child is StackLayout) {
                    ((StackLayout)child).ForceLayout ();
                    FixChildLayouts (child as Layout<View>);
                }
                if (child is AbsoluteLayout) {
                    ((AbsoluteLayout)child).ForceLayout ();
                    FixChildLayouts (child as Layout<View>);
                }
            }
        }
    }



    public class FastCellRenderer : ViewCellRenderer
    {
        public FastCellRenderer ()
        {
        }

        NSString cellId;
        private UIView bgView;

        public override UITableViewCell GetCell (Cell item, UITableViewCell reusableCell, UITableView tv)
        {
            cellId = cellId ?? new NSString (item.GetType ().FullName);
            var nativeCell = reusableCell as NativeCell;
            var viewCell = item as FastCell;

            if (reusableCell == null) {
                nativeCell = new NativeCell (cellId, viewCell);
            } else {
                nativeCell.RecycleCell (viewCell);
            }

            if (nativeCell != null) nativeCell.BackgroundColor = viewCell.BackgroundColor.ToUIColor();
            if (bgView == null)
            {
                bgView = new UIView();
                bgView.BackgroundColor = viewCell.SelectedBackgroundColor.ToUIColor();
            }

            nativeCell.SelectedBackgroundView = bgView;

            return nativeCell;
        }
    }
}


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>