Xamarin.iOS - 如何从 TableViewController 中隐藏或删除静态单元格 [英] Xamarin.iOS - How hide or delete a static cell from TableViewController

查看:22
本文介绍了Xamarin.iOS - 如何从 TableViewController 中隐藏或删除静态单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要隐藏或删除 TableView 中的静态行.

I need to hide or delete a static line from a TableView.

我尝试了几种方法,但没有任何效果.

I have tried several methods, but nothing works.

我试过了:

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();

    if ( TableView.NumberOfRowsInSection(0) == 9)
    {
        TableView.CellAt(NSIndexPath.FromItemSection(0, 0)).RemoveFromSuperview();
        TableView.ReloadRows(new NSIndexPath[] { TableView.IndexPathForCell(TableView.CellAt(NSIndexPath.FromItemSection(0, 0))) }, UITableViewRowAnimation.Fade);
    }
}

但是不起作用,单元格继续在 TableView 中.

But doesn't work, the cell continued in the TableView.

我试试这个:

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();

    if ( TableView.NumberOfRowsInSection(0) == 9)
    {
        TableView.DeleteRows(new NSIndexPath[] { NSIndexPath.FromItemSection(0, 0) }, UITableViewRowAnimation.None);
    }
}

但是抛出了这个异常:

Foundation.MonoTouchException:抛出的 Objective-C 异常.姓名:NSInternalInconsistencyException 原因:无效更新:无效第 0 节中的行数.更新后的现有部分 (9) 必须等于更新前包含在该节中的行 (9),加号或减号从该部分插入或删除的行数(插入 0,1 删除)并加上或减去移入或移出的行数该部分(0 移入,0 移出).

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (9) must be equal to the number of rows contained in that section before the update (9), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).

在那之后,我尝试了这个:

After that, I tried this:

public override void ViewDidLayoutSubviews()
{
    base.ViewDidLayoutSubviews();

    if ( TableView.NumberOfRowsInSection(0) == 9)
    {
        TableView.DataSource.CommitEditingStyle(TableView, UITableViewCellEditingStyle.Delete, NSIndexPath.FromItemSection(0, 0));
        TableView.DeleteRows(new NSIndexPath[] { NSIndexPath.FromItemSection(0, 0) }, UITableViewRowAnimation.None);
    }
}

但是现在抛出这个异常:

But now this exception is thrown:

Foundation.MonoTouchException:抛出的 Objective-C 异常.姓名:NSInvalidArgumentException 原因:-[DetalheContastableView:commitEditingStyle:forRowAtIndexPath:]: 无法识别选择器发送到实例 0x7fd49590

Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInvalidArgumentException Reason: -[DetalheContas tableView:commitEditingStyle:forRowAtIndexPath:]: unrecognized selector sent to instance 0x7fd49590

我该怎么办?

推荐答案

我不完全确定您为什么要尝试从 'ViewDidLayoutSubviews()' 中删除表格视图中的单元格,因为这将被多次调用,因此当您尝试删除它时,该单元格不会始终存在.我在这里假设它确实是您要删除的单元格.不管这里是使用淡入淡出"动画从表格中删除单元格的示例代码片段,我可以在上面看到您使用:

I'm not entirely sure why you're trying to remove a cell from your table view from the 'ViewDidLayoutSubviews()' as this will be called numerous times, and hence that cell will not always exist when you try to remove it. I am making an assumption here that it is indeed a cell that you're trying to delete. Irrespective here is an example code snippet for deleting a cell from a table using the 'fade' animation that I can see you using above:

YourTableSource.ReplaceSource(SomeData);

SomeTable.BeginUpdates();
SomeTable.DeleteRows(new NSIndexPath[] { NSIndexPath.FromItemSection(0, 0)}, UITableViewRowAnimation.Fade);
SomeTable.EndUpdates();

值得注意的是,您的源计数必须因您提交的删除次数而异.

It is worth noting that your source count must differ by the number of delete's that you are committing.

这篇关于Xamarin.iOS - 如何从 TableViewController 中隐藏或删除静态单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆