除了 UITableView 类中已经可用的标题之外,我目前正在为 UITableView 实现自定义标题(这样我们就可以同时在顶部 float 标题标题和副标题标题)。
为了做到这一点,我的表格分为许多部分,可以是:
- 标题部分,实际上是一个以空 UIView 作为标题的部分(因为我希望我的自定义标题 float 在该空 View 上)。
- 或者它可以是一个字幕部分,这是一个普通的部分,使用 iOS 提供的标题(当我们滚动时已经 float 在顶部的标题)。
两种类型的部分都可以在其中包含行,并且在我尝试使用 2 个自定义标题之前,实现工作完美。
当我尝试添加第二个自定义 header 时,调用 UITableView.RectForHeaderInSection
方法时出现此异常:
NSInternalInconsistencyException: request for rect of header in invalid section (6)
事情是我调用 this.UITableView.RectForHeaderInSection(6)
并且当异常弹出时(在完全相同的行上),我可以看到我的表至少有 7 个部分因此,第 6 部分不应无效。
我正在使用 RectForHeaderInSection
将我的 float 标题放在它应该在的位置。
有谁知道为什么它会崩溃,即使该部分不应该是无效的?或者也许是另一种获取我需要放置 float 标题的确切框架的方法(这将是我在标题部分创建的空 TableView 标题的框架)?
我只找到这个 question这可能与我的问题有关,但我认为在那种情况下问题的根源不同。
编辑:我意识到,当我说节数应该为 7 时,它是 UITableViewSource 子类中的节数,而不是表中的“实际”节数。是否有可能该部分已被删除或从未添加,即使它们已添加到源代码中?除了查看源代码中的列表之外,我如何查看表格中的部分数量?
我可能会在明天发布我的部分代码,因为现在我知道没有足够的信息来找出问题所在。
编辑 2:代码如下所示:
// first we create the custom header (Title)
var title1 = new CustomHeader();
var section1 = this.Table.TableSource.AddSection();
section1.HeaderView = title1.View;
// Fill section 1 with rows...
var section2 = this.Table.TableSource.AddSection();
// Fill section 2 with rows...
// We create the list of sections that our Title will contain
var title1Sections = new List<TableSection>();
title1Sections.Add(section1);
title1Sections.Add(section2);
// Now we call the method that will create our custom header
this.Table.TableSource.AddHeader(title1.View, title1Sections, this.Table.TableSource.Sections.IndexOf(section1);
因此,我们有第 1 部分,它有一个自定义标题标题, float 在第 1 部分和第 2 部分之上。第 2 部分有一个普通的字幕标题,当它们相互碰撞时,它会 float 在标题标题下面。现在是 AddHeader 方法:
public void AddHeader(UIView headerView, List<TableSection> sections, int indexOfFirstSection)
{
RectangleF rectForHeaderInTableView = this.TableView.RectForHeaderInSection(indexOfFirstSection); /* CRASHES HERE */
// Code that uses rectForHeaderInTableView to make our custom header float over everything...
}
如果我只有一个这样的自定义 header ,应用程序运行良好并且不会崩溃。但是,如果我以与此处相同的方式添加更多部分(比如第 3-4 部分)并添加自定义 header ,当它尝试添加第二个自定义 header 时,应用程序将在尝试获取 rectForHeaderInSection 时崩溃。如果我决定不为第 1-2 节设置自定义标题,而是为第 3-4 节设置自定义标题,它仍然有效。只要我只有一个自定义 header ,应用程序就不会崩溃。
最佳答案
您是对的,“有没有可能该部分已被删除或从未添加,即使它们已添加到源中?”。它们未添加到表中。
你必须调用:
ReloadData()
在您的 tableView 上将源与表的数据同步,以防止无效的部分索引。
关于ios - UITableView.RectForHeaderInSection 导致 'Request for rect of invalid section' 异常,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18261540/