【问题标题】:Updating TableView Header data更新 TableView 标头数据
【发布时间】:2019-05-22 19:35:56
【问题描述】:

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

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

【问题讨论】:

标签: c# uitableview xamarin.ios


【解决方案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();

【讨论】:

  • 我目前正在使用 GetViewForHeader 进行初始加载,所以我认为你让我朝着我需要继续前进的方向前进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多