naffie February 2016

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. Despite setting the background color to clear on the story board,the tableview, when testing on the iPad always has a gray background color. 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

cell.contentView.backgroundColor

But the GetCell function is never called.

EDIT

Here's a sample screen from one of the screens that has a static table view

screen shot

Could someone point me in the right direction?

Answers


Nick February 2016

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. Also you might want to check that your tableView source is being set to your CustomTableSource in ViewDidLoad:

tableView.Source = new CustomTableSource(this);

Unless you are using an IUITableViewDataSource then check that your Weak delegate and source being set:

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

Overriding 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:

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


naffie February 2016

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. 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.

Post Status

Asked in February 2016
Viewed 2,994 times
Voted 13
Answered 2 times

Search




Leave an answer