0

I need to change the selected item color of a listview in my Xamarin.Forms app. So I created a custom renderer...

PCL C#:

public class DarkViewCell : ViewCell {}

PCL XAML:

<ListView>
  <ListView.ItemTemplate>
    <DataTemplate>
      <local:DarkViewCell>
        <ViewCell.View>
          ... Stuff ...
        </ViewCell.View>
      </local:DarkViewCell>            
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

iOS

public class DarkViewCellRenderer : ViewCellRenderer
{
    private UIView bgView;

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
    {
        var cell = base.GetCell(item, reusableCell, tv);

        cell.BackgroundColor = UIColor.Black;
        cell.TextLabel.TextColor = UIColor.White;

        if (bgView == null)
        {
            bgView = new UIView(cell.SelectedBackgroundView.Bounds);
            bgView.Layer.BackgroundColor = UIColor.FromRGB(48,48,48).CGColor;
            bgView.Layer.BorderColor = UIColor.FromRGB(48, 48, 48).CGColor;
            bgView.Layer.BorderWidth = 2.0f;
        }

        cell.SelectedBackgroundView = bgView;

        return cell;
    }
}

But is not working. I also tried to change the SelectionStyle but nothing...

EDIT

In a new project it works. And the code is the same

4 Answers4

1

I'm not 100% sure but, I removed

<x:Arguments>
    <ListViewCachingStrategy>RecycleElement</ListViewCachingStrategy>
</x:Arguments>

and it started working.

0

Try setting:

public override UITableViewCell GetCell(Cell item, UITableView tv)
{
    var cell = base.GetCell(item, tv);
    cell.SelectedBackgroundView = new UIView() { BackgroundColor = UIColor.Black };
    return cell;
}

Update: I just used your code in a dummy project and it works as it should. Did you add the assembly attribute to register the custom renderer?

[assembly: ExportRenderer(typeof(DarkViewCell), typeof(DarkViewCellRenderer))]
namespace MyProject.iOS
{
    public class DarkViewCellRenderer : ViewCellRenderer
    {
    }
}
Steven Thewissen
  • 2,881
  • 13
  • 21
  • Is anything else in the custom renderer working or is it not working at all? I just copied that piece of code over from one of my own projects where it works exactly as needed. – Steven Thewissen Jul 12 '17 at 15:29
  • Updated my answer. – Steven Thewissen Jul 12 '17 at 16:48
  • Assembly attribute is there, breakpoints work as expected. I'm using the simulator could it be a bug? What are you using? – Matteo Bortolazzo Jul 13 '17 at 07:22
  • Also the simulator. You could try removing the tags, they're unnecessary but I doubt that will fix it. – Steven Thewissen Jul 13 '17 at 08:22
  • 1
    There is this line in the documentation as well: "For most Xamarin.Forms elements, it is optional to provide a custom renderer in each platform project. If a custom renderer isn't registered, then the default renderer for the control's base class will be used. However, custom renderers are required in each platform project when rendering a ViewCell element." - Do you also have an Android renderer for it if you have an Android platform project? – Steven Thewissen Jul 13 '17 at 08:24
  • No fix removing ViewCell.View – Matteo Bortolazzo Jul 13 '17 at 08:34
  • Android protected override Android.Views.View GetCellCore(Cell item, Android.Views.View convertView, ViewGroup parent, Context context) { return base.GetCellCore(item, convertView, parent, context); } – Matteo Bortolazzo Jul 13 '17 at 08:34
  • UWP public override Windows.UI.Xaml.DataTemplate GetTemplate(Cell cell) { return base.GetTemplate(cell); } – Matteo Bortolazzo Jul 13 '17 at 08:35
  • On a real device I have the same problem – Matteo Bortolazzo Jul 13 '17 at 08:35
0

I had a similar issue recently, I found the best way to set the background color of a cell is to use the cells ContentView

Try using the following in your GetCell method

cell.ContentView.BackgroundColor = UIColor.Black;
groveale
  • 425
  • 3
  • 10
0

Follow below code it's working for me

 public override UITableViewCell GetCell(Cell item, UITableView tv)
  {
    var cell = base.GetCell(item, tv);

     cell.SelectedBackgroundView = new UIView {
     BackgroundColor = UIColor.DarkGray,
   };
  return cell;
 }

Below link is useful for you

Xamarin.Forms ListView: Set the highlight color of a tapped item