Xamarin ios - 如何从静态 TableView 访问单元格

Xamarin ios - How to access a cell from a static TableView

我对 table 视图在 iPad 设备上的呈现方式有疑问。尽管在故事板上将背景颜色设置为清除,table 视图在 iPad 上测试时始终具有灰色背景颜色。我搜索了这个网站并找到了很多解决方案:

已经尝试过这个建议

public override void viewDidLoad() {
 base.ViewDidLoad();

tableView.backgroundView = new UIView();
tableView.backgroundView.backgroundColor = UIColor.Clear;

}

我什至尝试覆盖 table 视图 class 中的 GetCell 函数,这样我就可以通过访问单元格来更改 contentView 的颜色,就像这样

cell.contentView.backgroundColor

但从未调用 GetCell 函数。

编辑

这是其中一个具有静态 table 视图的屏幕示例

有人能给我指出正确的方向吗?

如果您想通过覆盖 GetCell 来更改背景颜色,您需要在故事板中为您的自定义单元格提供一个标识符,并覆盖您的 TableViews TableSource 中的 GetCell,如下所示。另外,您可能想检查您的 tableView 源是否在 ViewDidLoad:

中设置为您的 CustomTableSource
tableView.Source = new CustomTableSource(this);

除非您使用的是 IUITableViewDataSource,否则请检查您的 Weak 委托和来源是否已设置:

tableView.WeakDelegate = this;
tableView.WeakDataSource = this;

覆盖 GetCell:

public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
     var cell = tableView.DequeueReusableCell("CellIdentifier") as CustomCell;
     if (cell == null)
            cell = new CustomCell();
     cell.BackgroundColor = UIColor.Clear
     return cell;
}

您还必须覆盖 RowsInSection:

public nint RowsInSection(UITableView tableView, nint section)
{
      return 7;
}

在尝试解决这个白色背景问题两天后,我找到了对我有用的最简单的解决方案:

要解决它,只需将此行添加到 AppDelegate.cs 文件中的 FinishedLaunching 函数中。这定义了您应用中所有 table 单元格的外观(如果您确实希望所有 table 视图单元格都具有清晰的背景。)

    UITableViewCell.Appearance.BackgroundColor = UIColor.Clear;

现在可以在 iPhone 和 iPad 上正确呈现。