更新TableView标头数据

时间:2019-05-22 19:35:57

标签: c# uitableview xamarin.ios

我有一个表视图,该表视图的标头包含从API填充的数据。如果发生某些变化(例如,如果他们更改了自己的个人资料照片),我将需要一种更新这些值的方法

我目前正在从GetViewForHeader()设置初始数据,但是我不知道更改后如何轻松地更新标头数据。

1 个答案:

答案 0 :(得分:1)

UITableView没有任何方法可以仅重新加载标题视图/标题...您必须重新加载整个节或表:

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

table.ReloadData();

另一种方法是获取要更新的控件的引用:

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;
    }
}

要访问该控件的任何地方:

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.
    }

我尝试调用注释中提到的SetNeedsDisplay,但是没有成功,我发现在GetViewForHeader中执行了代码,但是视图对我没有更新,也许我如果您错过了一些东西,可以尝试一下:

 UIView v  = s.GetViewForHeader(table, 0);
 v.SetNeedsDisplay();