简体   繁体   English

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

[英]Xamarin ios - How to access a cell from a static TableView

I have an issue with how my tableview is rendered on an iPad device. 我在iPad设备上如何呈现表格视图时遇到问题。 Despite setting the background color to clear on the story board,the tableview, when testing on the iPad always has a gray background color. 在iPad上进行测试时,尽管将故事板上的背景颜色设置为透明,但在桌面视图上始终具有灰色背景颜色。 I have searched around this site and found plenty of solutions: 我在这个网站上进行了搜索,发现了很多解决方案:

Already tried this suggestion 已经尝试过此建议

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

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

} }

And I even tried to override the GetCell function inside my tableview class, so I can change the contentView color by accessing the cell, like this 我什至尝试覆盖tableview类中的GetCell函数,因此我可以通过访问单元格来更改contentView颜色,如下所示

cell.contentView.backgroundColor

But the GetCell function is never called. 但是从未调用GetCell函数。

EDIT 编辑

Here's a sample screen from one of the screens that has a static table view 这是其中一个屏幕具有静态表格视图的示例屏幕

屏幕截图

Could someone point me in the right direction? 有人可以指出我正确的方向吗?

If you want to change the background color by overriding GetCell you want to give your custom cells an identifier in the storyboard and override GetCell in your TableViews TableSource like below. 如果要通过覆盖GetCell来更改背景颜色,则要在故事板上为自定义单元格提供一个标识符,并在TableViews TableSource中覆盖GetCell,如下所示。 Also you might want to check that your tableView source is being set to your CustomTableSource in ViewDidLoad: 另外,您可能想检查您的tableView源是否已设置为ViewDidLoad中的CustomTableSource:

tableView.Source = new CustomTableSource(this);

Unless you are using an IUITableViewDataSource then check that your Weak delegate and source being set: 除非您使用的是IUITableViewDataSource否则请检查是否已设置弱IUITableViewDataSource和源:

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

Overriding GetCell: 覆盖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;
}

You'll have to override RowsInSection as well: 您还必须重写RowsInSection:

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

After two days trying to solve this white background issue, I found the simplest solution that worked for me: 在尝试解决此白色背景问题两天后,我找到了最简单的解决方案:

To solve it, simply add this line in your AppDelegate.cs file,in your FinishedLaunching function. 要解决此问题,只需在FinishedLaunching函数的AppDelegate.cs文件中添加此行。 This defines the appearance of all the table cells in your app (if you actually want all your tableview cells to have a clear background.) 这定义了应用程序中所有表格单元格的外观(如果您实际上希望所有表格视图单元格都具有清晰的背景)。

    UITableViewCell.Appearance.BackgroundColor = UIColor.Clear;

This is now rendered correctly on the iPhone as well as the iPad. 现在,可以在iPhone和iPad上正确渲染了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM