Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ItemAppearing not called in custom ViewCell

JoshuaNovakJoshuaNovak USMember ✭✭

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;
        }
    }
}

Posts

  • adamkempadamkemp USInsider, Developer Group Leader mod

    You're probably interfering with the default behavior by using a custom renderer. I would look in the assembly browser at the default cell renderers and see how they handle calling the appearance methods.

  • GeorgeCookGeorgeCook PEUniversity ✭✭✭
    edited July 2015

    I've not designed fastcell to use ViewAppearing. I've never had a need for it. Everything I could want to do I can achieve using the SetupCell method of the FastCell class.

    What is it you need to do? Another user asked if I could support it for facilitating endless scrolling; but that's something that should be implemented at the level of the list itself, not the cell (i.e. in response to list scrolling events).

    I don't see what the use cases are for view appearing/disappearing, if there's a setup cell method - the only thing I can think (and will add) is a method to say "the cell is going to change"; like cellWillBeRecycled (for removing any event listeners, cancelling requests, etc , I guess).

    Please take a moment to explain (with code) what it is you are expecting to do.

  • JoshuaNovakJoshuaNovak USMember ✭✭

    @GeorgeCook I'm actually the same user that asked in the github issue. Basically I need to implement endless scrolling and i'd like the cells to scale from 0.9 to 1 as they come into view (similar to the iShows app). Do you have an example of how to use setupcell and/or handle scrolling events on the list? I can't seem to find any, which is why I used ItemAppearing to begin with.

  • GeorgeCookGeorgeCook PEUniversity ✭✭✭

    I have no examples showing how to do that for infinite scrolling; because I think that's just plain bad design. It's not good encapsulation at all. The idea of a view cell loading more of it's parent listviews contents is plain ugly to me; but if you don't mind that, there's nothing stopping you checking the location of the BindingContext in the datasource list, then reloading it (yuk!).

    A much better solution is to add scroll awareness to the lists (I've done this and will add it to our public code library), and then check the scroll event for % completion of the lists contentview.

    When I get a chance I'll add that.

    I've also noticed that you're looking at an out of date repo. Apologies I didn't spot that before. Our current code repo is here:

    https://github.com/twintechs/TwinTechsFormsLib

    You should read the accompanying blog posts about this : http://blog.twintechs.com/how-to-achieve-native-list-performance-with-xamarin-forms-listview

    I've many examples on how to use setupcell in the examples project; it's just a helper wrapper around BindingContext changed anyhow.

    This will give you a better understanding of what you're doing. Any other questions, please put through the git repo at : https://github.com/twintechs/TwinTechsFormsLib

    I'll delete the other one when I get a moment.

    Please note the twintechs forms project currently has library code in the example project; this is becuase of a hugely irritating xamarin studio bug that makes it impossible to correctly debug inside of referred PCLs.. :/

    I'm supposing that you will just copy the relevant files anyway. If not, wait a few days and I will tidy up the repo when I share the cross platform gestures I've been toiling with :)

Sign In or Register to comment.