0

I have a tableview that has a header that contains data that's populated from an API. I'll need a way to update those values if something changed (for example if they changed their profile photo)

I'm currently setting the initial data from GetViewForHeader() but I don't know how I can easily update the header data when a change is made.

Eman
  • 1,093
  • 2
  • 26
  • 49
  • 1
    https://stackoverflow.com/questions/8732203/is-there-a-way-to-force-a-refresh-of-just-a-single-header-in-uitableview?lq=1 – Jason May 23 '19 at 01:05

1 Answers1

1

UITableView does not have any method to reload only the header view/title ...You have to reload the whole section or the table:

table.ReloadSections(new NSIndexSet(0), UITableViewRowAnimation.Automatic); 

Or

table.ReloadData();

Another way is get a reference to the control you want to update:

public class TableSource : UITableViewSource
{

    public UILabel testLabel;



    public override UIView GetViewForHeader(UITableView tableView, nint section)
    {

        UILabel lab = new UILabel();

        lab.Text = "123";
        lab.Frame = new CGRect(0,0,100,50);

        //Get reference
        testLabel = lab;

        return lab;
    }

    public override nfloat GetHeightForHeader(UITableView tableView, nint section)
    {
        return 50;
    }
}

At anywhere you want to access the control:

public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        // Perform any additional setup after loading the view, typically from a nib.

        UITableView table = new UITableView(View.Bounds); // defaults to Plain style
        string[] tableItems = new string[] { "Vegetables", "Fruits", "Flower Buds", "Legumes", "Bulbs", "Tubers" };

        TableSource s = new TableSource(tableItems);
        table.Source = s;
        Add(table);


        //get the reference 

        UILabel myLabel = s.testLabel;
        //update here.
    }

I tried call SetNeedsDisplay as mentioned in the comment, but I did not get success, I found the code executed in the GetViewForHeader, however the view doesn't update for me, maybe I'm missing something, you can have a try:

 UIView v  = s.GetViewForHeader(table, 0);
 v.SetNeedsDisplay();
nevermore
  • 15,432
  • 1
  • 12
  • 30
  • I'm using GetViewForHeader currently for the inital load so I think your getting me in the direction I need to keep going. – Eman May 23 '19 at 15:00