C# 未激发Xamarin UITableViewDelegate滚动事件
我有一个UITableView,其源被设置为UITableViewSource子类。目前我有多种方法在使用,其中包括一些显而易见的方法;NumberOfSection、GetCells、RowsInSection、RowsSelected等 今天早上,在表的顶部实现刷新视图时,我尝试实现scrollViewDidScroll(Scroll())方法,但是当表视图滚动时,没有调用它。没有抛出异常,也没有命中断点-但是当我滚动表视图时,控制台中偶尔会出现一个断言失败(但不是每次): 12月18日12:50:02 iMac断言[26037]:断言失败:15B42 13C75:断言+12188[8CF1968D-3466-38B7-B225-3F6F5B64C552]:0x1 表格视图源在ViewController中设置如下: Source=dts=newdashboardtablesource() 在TableView中,源代码如下所示: 使用基础; 使用制度; 使用System.CodeDom.Compiler; 使用UIKit; 使用System.Collections.Generic; 使用核心图形; 使用视频工具箱C# 未激发Xamarin UITableViewDelegate滚动事件,c#,ios,xamarin,C#,Ios,Xamarin,我有一个UITableView,其源被设置为UITableViewSource子类。目前我有多种方法在使用,其中包括一些显而易见的方法;NumberOfSection、GetCells、RowsInSection、RowsSelected等 今天早上,在表的顶部实现刷新视图时,我尝试实现scrollViewDidScroll(Scroll())方法,但是当表视图滚动时,没有调用它。没有抛出异常,也没有命中断点-但是当我滚动表视图时,控制台中偶尔会出现一个断言失败(但不是每次): 12月18日12
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)
{
}
}
}
应用程序中没有其他正在侦听表视图滚动的事件,因此我无法真正理解为什么没有触发此事件。任何帮助都将不胜感激
谢谢。除了一件事之外,您的代码似乎很好。问题实际上在于您的
GetCell
方法
首先,我假设DashboardTableCell
是UITableVIewCell
的一个子类。如果是这样的话,在这一行
新建UITableViewCell(UITableViewCellStyle.Default,CellIndentifer)作为DashboardTableCell
您试图将基类强制转换为子类,该子类可以(在我的测试中确实)返回null。
as
运算符在强制转换未成功时返回null,而不是引发异常
引自
as运算符类似于强制转换操作。但是,如果转换不可能,as将返回null而不是引发异常
若单元格为null,则返回null。返回null也会导致断言失败
因此,您可以简单地将UITableViewCell
替换为DashboardTableCell
并将as
删除如下:newdashboardtablecell(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;
}
如果这不能解决您的问题,我将再次检查您的代码:)除了一件事之外,您的代码似乎很好。问题实际上在于您的GetCell
方法
首先,我假设DashboardTableCell
是UITableVIewCell
的一个子类。如果是这样的话,在这一行
新建UITableViewCell(UITableViewCellStyle.Default,CellIndentifer)作为DashboardTableCell
您试图将基类强制转换为子类,该子类可以(在我的测试中确实)返回null。
as
运算符在强制转换未成功时返回null,而不是引发异常
引自
as运算符类似于强制转换操作。但是,如果转换不可能,as将返回null而不是引发异常
若单元格为null,则返回null。返回null也会导致断言失败
因此,您可以简单地将UITableViewCell
替换为DashboardTableCell
并将as
删除如下:newdashboardtablecell(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;
}
如果这不能解决您的问题,我将再次查看您的代码:)你好,安德瑞斯,谢谢你-我没有意识到我没有创建自定义单元格的新实例!有第二双眼睛总是好的!我已经更改了代码,以便它在DashboardTableCell类中使用构造函数,该类是:public DashboardTableCell(UITableViewCellStyle,string ReuseIdentifier):base(style,ReuseIdentifier){base.Init();}但是它在运行时崩溃,因为没有IntPtr参数,然而,在我看来,没有一个基构造函数接受3个参数(style、ident和IntPtr)?在我的例子中,完全相同的场景可以很好地工作。能否提供一些仪表板TableCell的代码。DashboardTableCell是UITableViewCell的子类还是基类为UITableViewCell的其他类?同时,您可以尝试以下方法:public DashboardTableCell(IntPtr handle):base(handle){}Hi andrius,谢谢-我没有意识到我没有创建自定义单元格的新实例!有第二双眼睛总是好的!我已经更改了代码,以便它在DashboardTableCell类中使用构造函数,该类是:public DashboardTableCell(UITableViewCellStyle,string ReuseIdentifier):base(style,ReuseIdentifier){base.Init();}但是它在运行时崩溃,因为没有IntPtr参数,然而,在我看来,没有一个基构造函数接受3个参数(style、ident和IntPtr)?在我的例子中,完全相同的场景可以很好地工作。能否提供一些仪表板TableCell的代码。DashboardTableCell是UITableViewCell的子类还是基类为UITableViewCell的其他类?同时,您可以尝试以下方法:public DashboardTableCell(IntPtr handle):base(handle){}