How to use cell reuse in UITableViewSource in Xamarin?

I have a class UITableViewSource

that is used for UISearchDisplayController

. In my main table view, I have a custom cell that works great. Now I want to use a custom cell to represent the search results table as well. But I only manage it to display the default style with

public class SearchSource : UITableViewSource
{
    static NSString cellIdentifier = new NSString("CustomCell");

    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        UITableViewCell cell = tableView.DequeueReusableCell (cellIdentifier);

        if (cell == null)
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);

        cell.TextLabel.Text = myList [indexPath.Row].Name;
        return cell;
    }

      

But here I only have a standard cell and I need my custom cell. A custom cell in the master table view uses the following code:

public partial class TestViewController : UITableViewController
{
    static NSString cellIdentifier = new NSString("CustomCell");

    public TestViewController (IntPtr handle) : base (handle)
    {
        TableView.RegisterClassForCellReuse (typeof(CustomCell), cellIdentifier);
    }

    public class TableSource : UITableViewSource {

        public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath){
            CustomCell cell = (CustomCell)tableView.DequeueReusableCell (cellIdentifier);
            cell.UpdateCell(...);
    }
}

      

Here's the constructor CustomCell

:

public CustomCell (IntPtr handle) : base(handle)

This is how I create UISearchDisplayController

:

// create search controller
searchBar = new UISearchBar (new RectangleF (0, 0, View.Frame.Width, 50)) {
    Placeholder = "Enter a search query"
};
searchController = new UISearchDisplayController (searchBar, this);
searchController.Delegate = new SearchDelegate (tableItems);
searchController.SearchResultsSource = new SearchSource (searchController);
this.TableView.TableHeaderView = searchBar;

      

But everything I'm trying is causing the app to crash (no link, can't deactivate ...) or constructor mismatch.

How can I use my custom cell for the generated table view UISearchDisplayController

?

+3


source to share


1 answer


Switching to the new iOS 6 cell reuse pattern I have adapted mine SearchSource

to

CustomPatientCell cell = (CustomCell)tableView.DequeueReusableCell (cellIdentifier);

      

and I added the following to TestViewController

:



searchController.SearchResultsTableView.RegisterClassForCellReuse(typeof(CustomCell), cellIdentifier);

      

I needed to add a few methods like GetHeightForRow

mine SearchSource

to make this work.

Now I have two similar implementations TableSource

and SearchSource

- perhaps it is possible to sum both? I have seen from other implementations that methods such as the numberOfRowsInSection

request that is currently active. How can I sum this up as well?

+1


source







All Articles