Xamarin UITableViewDelegate滚动事件未激发

本文关键字:事件 UITableViewDelegate 滚动 Xamarin | 更新日期: 2023-09-27 18:27:16

请注意,这是Xamarin论坛上论坛问题的副本。

我有一个UITableView,它的源设置为UITableViewSource子类。我目前有多种方法在使用,其中包括显而易见的方法;节数、GetCells、RowsInSection、RowsSelected等

今天早上,当我在表的顶部实现刷新视图时,我试图实现scrollViewDidSoll(Scroll())方法,但当表视图滚动时,它没有被调用。没有抛出异常,也没有碰到断点,但当我滚动表视图时,控制台中偶尔会出现一个断言失败(但不是每次):12月18日12:50:02 iMac已分配[26037]:断言失败:15B42 13C75:已分配+12188[8CF1968D-3466-38B7-B225-3F6F5B64C552]:0x1

表视图源在ViewController中设置如下:workedTableView.Source=dts=新的DashboardTableSource();

在TableViewSource中的代码如下:使用Foundation;使用系统;使用System.CodeDom.Compiler;使用UIKit;使用System.Collections.Generic;使用CoreGraphics;使用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)
        {
        }
    }
}

应用程序中没有其他事件在侦听表视图滚动,所以我真的不明白为什么没有触发此事件。任何帮助都将不胜感激!

谢谢。

Xamarin UITableViewDelegate滚动事件未激发

您的代码似乎很好,除了一件事。问题实际上在于你的GetCell方法。

首先假设DashboardTableCellUITableVIewCell的一个子类。如果是这样的话,在这行new UITableViewCell (UITableViewCellStyle.Default, CellIndentifer) as DashboardTableCell您正在尝试将基类强制转换为子类,该子类可以(在我的测试中)返回null。如果强制转换不成功,as运算符返回null,而不是抛出异常

报价来源https://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

as运算符类似于强制转换操作。但是,如果转换不可能,as将返回null,而不是引发异常。

然后,若单元格为null,则返回null。返回null也导致我的断言失败。

因此,您可以简单地用DashboardTableCell替换UITableViewCell,并删除as,如下所示:new DashboardTableCell (UITableViewCellStyle.Default, CellIndentifer);

但这种方式在我看来更干净:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
{
    var cell = tableView.DequeueReusableCell (CellIndentifer) as DashboardTableCell;
    if (cell == null) {
         // Notice that we are creating an instance of DashboardTableCell
         // instead of UITableViewCell
         cell = new DashboardTableCell (UITableViewCellStyle.Default, CellIndentifer);
    }
    // cell is definitely not null now. You can update and return
    cell.SetupCell (dataLoaded, indexPath);
    return cell;
}

如果这不能解决您的问题,我将再次查看您的代码:)