MAUI: Enhanced listview for iOS plaform

Sreejith Sreenivasan 1,001 Reputation points
2023-11-15T14:10:51.64+00:00

I have an enhanced listview (Helpers) which is working fine on Xamarin Forms project. But when I migrate it to MAUI my app is breaking with below exception.

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Maui.Controls.Compatibility.Platform.iOS.CellTableViewCell.GetNativeCell(UITableView , Cell , Boolean , String ) at Microsoft.Maui.Controls.Compatibility.Platform.iOS.ListViewRenderer.ListViewDataSource.GetCell(UITableView , NSIndexPath ) at UIKit.UIApplication.UIApplicationMain(Int32 , String[] , IntPtr , IntPtr ) at UIKit.UIApplication.Main(String[] , Type , Type ) at MyProject.Program.Main(String[] args) in /Users/MyCompany/Projects/MyProject-maui/MyProject/Platforms/iOS/Program.cs:line 13

Screenshot

Screen Shot 2023-11-15 at 5.27.28 AM

My Code:

SingleProject

public class EnhancedListView : ListView
{
    public EnhancedListView()
    {
    }
    public void ForceNativeTableUpdate()
    {
        ViewCellSizeChangedEvent?.Invoke();
    }
    public event Action ViewCellSizeChangedEvent;
}

iOS Platform

class EnhancedListViewRenderer : ListViewRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<ListView> e)
    {
        base.OnElementChanged(e);
        if (e.NewElement is EnhancedListView enhancedListView)
        {
            enhancedListView.ViewCellSizeChangedEvent += UpdateTableView;
        }
        if(Control != null)
        {
            Control.SectionHeaderTopPadding = new nfloat(0);
        }
    }
    private void UpdateTableView()
    {
        if (!(Control is UITableView tv)) return;
        tv.BeginUpdates();
        tv.EndUpdates();
    }
}

Android part is working fine without any enhanced renderers. It is expanding and collapsing when data is visible on the UI.

Developer technologies | .NET | .NET MAUI
{count} votes

Answer accepted by question author
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,151 Reputation points Microsoft External Staff
    2023-11-20T07:49:24.8666667+00:00

    Hello,

    Thanks for your feedback.

    After testing, your code will work fine on iOS. Therefore, the cause of this issue is more likely to be an issue with the development tool.

    In addition, using the Renderer in MAUI should comply with the specifications in this document.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Grigory Smyslov 0 Reputation points
    2024-02-28T14:25:00.9433333+00:00

    I faced with the same issue. After some investigation I found out that there are two ListViewRenderer classes. I used the Microsoft.Maui.Controls.Compatibility.Platform.iOS.ListViewRenderer as a base for my own renderer - and it causes an error. When I changed it to the Microsoft.Maui.Controls.Handlers.Compatibility.ListViewRenderer, the error was gone.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.