UITableViewSource scrollViewDidScroll not called

JSpringJSpring GBUniversity ✭✭

Hi,

I have a UITableView of which the source is set to a UITableViewSource sub class. I have multiple methods in use at the moment, amongst the obvious ones; NumberOfSections, GetCells, RowsInSection, RowsSelected etc.

This morning whilst implementing a refresh view at the top of the table I tried to implement the scrollViewDidScroll (Scroll()) method, however it is not being called when the table view scrolls. There is no exception being thrown, nor is it hitting the breakpoint - but there is an assertion failed which appears in the console occasionally when I scroll the table view (not every time though):
Dec 18 12:50:02 Jaydens-iMac assertiond[26037]: assertion failed: 15B42 13C75: assertiond + 12188 [8CF1968D-3466-38B7-B225-3F6F5B64C552]: 0x1

The table view source is set in the ViewController like so:
journeyTableView.Source = dts = new DashboardTableSource ();

And within the TableViewSource code is as follows:
using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
using System.Collections.Generic;
using CoreGraphics;
using VideoToolbox;

namespace AppName
{
    public class DashboardTableSource : UITableViewSource
    {
        public const string CellIndentifer = "DriveCell";

        #region Refresh View Related
        public override void Scrolled (UIScrollView scrollView)
        {
//          Globals.refreshView.DidScroll ();
            Console.WriteLine ("TV Scrolled!");
        }
        #endregion

        public override nint NumberOfSections (UITableView tableView)
        {
            if(!dataLoaded)
            {
                var rect = new CGRect (0, 0, tableView.Bounds.Width, tableView.Bounds.Height);

                UIImageView iv = new UIImageView(tableView.Bounds);
                iv.Image = UIImage.FromBundle ("NoData");
                iv.ContentMode = UIViewContentMode.Center;

                tableView.BackgroundView = iv;

                return 0;
            }
            else
            {
                UIView.Animate (0.5, ()=>{
                    if(tableView.BackgroundView != null){
                        tableView.BackgroundView.Alpha = 0.0f;
                        tableView.BackgroundView = null;
                    }
                });

                return 1;
            }

        }

        public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            var cell = tableView.DequeueReusableCell (CellIndentifer) as DashboardTableCell ??
                       new UITableViewCell (UITableViewCellStyle.Default, CellIndentifer) as DashboardTableCell;
            if (cell != null) {
                cell.SetupCell (dataLoaded, indexPath);
                return cell; 
            } else {
                return null;
            }
        }

        public override nint RowsInSection (UITableView tableview, nint section)
        {
            if (!dataLoaded)
                return 0;
            else
                return obj.Count;
        }

        public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
        {
            return true;
        }

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            var cell = tableView.CellAt (indexPath) as DashboardTableCell;
            cell.SetSelectedState (true, true);

            SelectedRow = indexPath.Row;
        }

        public override void RowDeselected (UITableView tableView, NSIndexPath indexPath)
        {
        }
    }
}

There are no other events which are listening for a table view scroll within the application, so I cannot really understand as to why this event is not being fired. Any help would be appreciated!

Thanks.

Tagged:

Posts

  • VitorLeitaoVitorLeitao USMember

    On our case we needed to inherit also from IUIScrollViewDelegate and on the
    private void Scrolled(UIScrollView scrollView) add the attribute:
    [Export("scrollViewDidScroll:")]

    After this it worked normal. Try to add only the attribute at first

  • JSpringJSpring GBUniversity ✭✭

    Thanks, will give this a go soon!

  • JSpringJSpring GBUniversity ✭✭

    Thanks, this works if I am working within a UITableViewController but doesn't seem to in a nested UITableView in a UIView.

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    You can use the Scrolled event:

    this.TableView.Scrolled += TableViewScrolled;

    Please note that the events will only be called if you have not set a delegate.

  • JSpringJSpring GBUniversity ✭✭
    edited January 2016

    Hi @rene_ruppert - I have a delegate set therefore cant use the Scrolled event. However since UITableViewSource derives from UIScrollViewDelegate one would think overriding the Scrolled event would work.

    Here is what I have so far:

       #region Underlying Scroll View Related
        [Export("scrollViewDidScroll:")]
        private void ScrollViewDidScroll (UIScrollView scrollView)
        {
            var currentOffset = scrollView.ContentOffset.Y;
            var maxOffset = scrollView.ContentSize.Height - scrollView.Frame.Size.Height;
    
            float bottomPadding = 10.0f; // This should be equal to the cell height. Then can set to x amt of cells early
            if(maxOffset - currentOffset <= bottomPadding)
            {
                Console.WriteLine ("At Bottom Of The Scroll View / Table");
                if(tv != null)
                    launchReload (tv, null);
            }
        }
    
        public override void Scrolled (UIScrollView scrollView)
        {
            ScrollViewDidScroll (scrollView);
        }
        #endregion
    

    Following from a discussion here

  • rene_ruppertrene_ruppert DEXamarin Team, University, XamUProfessors Xamurai

    I made a quick sample and it is working as expected. I get a lot of output "Scrolling".

    class TheSource : UITableViewSource
        {
            #region implemented abstract members of UITableViewSource
            public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
            {
                var cell = new UITableViewCell(UITableViewCellStyle.Default, "reuseId");
    
                cell.TextLabel.Text = "Some cell " + indexPath.Row;
                return cell;
            }
            public override nint RowsInSection (UITableView tableview, nint section)
            {
                return 100;
            }
            #endregion
    
            public override void Scrolled (UIScrollView scrollView)
            {
                Console.WriteLine("Scrolling");
            }
    
Sign In or Register to comment.