ios - 未调用 GetHeightForHeader 和 GetViewForHeader

标签 ios uitableview xamarin

我正在处理一个包含两个部分的表格 View 。我想在第二部分标题中添加一个 headerView。

我已经实现了以下代码。它调用 TitleForHeader 但不幸的是 GetHeightForHeaderGetViewForHeader 都没有被调用。有人知道吗?

public override string TitleForHeader (UITableView tableView, nint section)
{
   if (section == 0)
   {
     return "Details";
   }
   if (section == 1)
   {
     return "Settings";
   }

 return "";
}

public override nfloat GetHeightForHeader(UITableView tableView, nint section)
{
    if (section == 0)
    {
       return 0.0001f;
    }
    if (section == 1) 
    {
        return 100.0f;
    }
  return UITableView.AutomaticDimension;
}

public override UIView GetViewForHeader(UITableView tableView, nint section)
{
  if (section == 1) {
    UIView headerView = new UIView (new CGRect (0, 0, (float)tableView.Bounds.Width, (float)tableView.SectionHeaderHeight));
    headerView.BackgroundColor = UIColor.Black;
    return headerView;
   }
   else {
      return null;
   }
}

最佳答案

我遇到了同样的问题,只是意识到我正在像这样设置委托(delegate)和源:

Table.Delegate = new TableDelegate(this); //remove this bit
Table.Source = new TableSource<string>(list, Table);

我意识到我应该只使用 UITableViewSource 而不是同时设置两者。

UITableViewSource replaces the following two classes, which are still available in Xamarin.iOS but are not normally required:

UITableViewDataSource – An Objective-C protocol that is modeled in Xamarin.iOS as an abstract class. Must be subclassed to provide a table with a view for each cell, as well as information about headers, footers and the number of rows and sections in the table.

UITableViewDelegate – An Objective-C protocol that is modeled in Xamarin.iOS as a class. Handles selection, editing features and other optional table features.

Xamarin docs

我刚刚将我的 rowselected 方法移动到我的 TableSource 中,它现在调用 TitleForHeaderGetViewForHeader 方法(它们不是相互排斥的)并且设置源无委托(delegate)。

关于ios - 未调用 GetHeightForHeader 和 GetViewForHeader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34665313/

相关文章:

ios - swift : TableView Cells not getting selected correctly

ios - 查找包含特定字符串的对象的索引

ios - 以编程方式更改 UITabBarController 选项卡栏属性的类

c# - 如何获取 webapi 对象列表?

ios - 滚动到顶部后未调用 TodayExtension 的 ViewWillAppear

iphone - 从具有多个部分的 UITableView 中删除行

ios - 在没有任何 Storyboard引用的情况下在 Controller 之间传递数据

ios - 将 UIViewController 更改为 UITableViewController

xamarin - Xamarin表单-CollectionView不会触发SelectionChangedCommand

android - 如何绑定(bind) TextView 的 TextColor?