Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

ListView item highlighting doesn't work on iOS

VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
edited October 2019 in Xamarin.Forms

After updating to the newest version of Xamarin.Forms none of ViewCells can be selected no more(on iOS. on Android everything is fine). To make sure, that this error wasn't in my code i've created a new blank solution and tested simple ListView and get same glitch. I only see 1px height blue line when item is selected.

<StackLayout> <Label Text="Welcome to Xamarin.Forms!" HorizontalOptions="Start" VerticalOptions="CenterAndExpand" /> <ListView ItemsSource="{Binding Items}" SelectionMode="Single" SeparatorColor="Red"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding .}"/> </DataTemplate> </ListView.ItemTemplate> </ListView> </StackLayout>

Best Answers

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    Accepted Answer

    Okay. I've rolled back to Xamarin.Forms 3.5.0.274416 and Xamarin.Essentials 1.2.0 and selection works like a charm.

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    edited October 2019 Accepted Answer

    So here i am. To fix this problem i've created Custom Renderer. But for ListView, not ViewCell(but it also can be fixed with custom renderer for ViewCell ).

    Here is the code:
    [assembly: ExportRenderer(typeof(ListView), typeof(StandardListViewRenderer))] namespace App.CrossPlatform.iOS.CustomRenderers { public class StandardListViewRenderer : ListViewRenderer { protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { try { base.OnElementPropertyChanged(sender, e); if (Control == null) return; if (!((ListView)sender).IsRefreshing) { var selection = Control.IndexPathForSelectedRow; Control.ReloadData(); if (selection != null) { var row = Control.CellAt(selection); row.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); row.SelectionStyle = UITableViewCellSelectionStyle.Blue; Control.SelectRow(selection, false, UITableViewScrollPosition.None); } } } catch { // ignored } } } }

    As far as i understand ContentView's background colour is no more Transparent, so selection can't be visible. After setting it to transparent everything works like a charm.

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    Accepted Answer

    This is code if you'd like to use Custom Renderer for ViewCell, not ListView:

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { var cell = base.GetCell(item, reusableCell, tv); cell.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; return cell; }

Answers

  • GraverobberGraverobber Member ✭✭✭
    edited October 2019

    I had the same problem but didn't even realise it was a bug. I just thought I messed something up.
    If you want to report it I think you can do that here https://github.com/xamarin/xamarin.forms/issues

    However, for me I solved it by giving my custom item cell a Background and in its model a IsSelected state.
    Bind the Background to a Color property and return your unselected and selected color in its get according to the IsSelected state.

  • ColeXColeX Member, Xamarin Team Xamurai

    I change TextCell to ViewCell and change Selection Style in custom renderer like below , but still no luck , it should be the issue .

    [assembly: ExportRenderer(typeof(ViewCell), typeof(MyRenderer))]
    namespace FormsApp.iOS
    {
        class MyRenderer : ViewCellRenderer
        {
            public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv)
            {
                var cell = base.GetCell(item, reusableCell, tv);
                if (cell != null)
                {
                    cell.SelectionStyle = UITableViewCellSelectionStyle.Blue;
                }
                return cell;
            }
        }
    }
    

    I suggest you raise issue here : https://github.com/xamarin/Xamarin.Forms/issues .

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    Accepted Answer

    Okay. I've rolled back to Xamarin.Forms 3.5.0.274416 and Xamarin.Essentials 1.2.0 and selection works like a charm.

  • ColeXColeX Member, Xamarin Team Xamurai

    @VolodymyrSavchenko said:
    Okay. I've rolled back to Xamarin.Forms 3.5.0.274416 and Xamarin.Essentials 1.2.0 and selection works like a charm.

    Please mark it as answer though it's not a nice solution ..

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    edited October 2019 Accepted Answer

    So here i am. To fix this problem i've created Custom Renderer. But for ListView, not ViewCell(but it also can be fixed with custom renderer for ViewCell ).

    Here is the code:
    [assembly: ExportRenderer(typeof(ListView), typeof(StandardListViewRenderer))] namespace App.CrossPlatform.iOS.CustomRenderers { public class StandardListViewRenderer : ListViewRenderer { protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e) { try { base.OnElementPropertyChanged(sender, e); if (Control == null) return; if (!((ListView)sender).IsRefreshing) { var selection = Control.IndexPathForSelectedRow; Control.ReloadData(); if (selection != null) { var row = Control.CellAt(selection); row.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); row.SelectionStyle = UITableViewCellSelectionStyle.Blue; Control.SelectRow(selection, false, UITableViewScrollPosition.None); } } } catch { // ignored } } } }

    As far as i understand ContentView's background colour is no more Transparent, so selection can't be visible. After setting it to transparent everything works like a charm.

  • VolodymyrSavchenkoVolodymyrSavchenko UAMember ✭✭✭
    Accepted Answer

    This is code if you'd like to use Custom Renderer for ViewCell, not ListView:

    public override UITableViewCell GetCell(Cell item, UITableViewCell reusableCell, UITableView tv) { var cell = base.GetCell(item, reusableCell, tv); cell.ContentView.BackgroundColor = new UIColor(0, 0, 0, 0f); cell.SelectionStyle = UITableViewCellSelectionStyle.Blue; return cell; }

Sign In or Register to comment.