单击按钮更新包含新数据的表格视图

时间:2014-08-04 13:35:45

标签: c# uitableview uibutton xamarin

我有一个带有邮政编码的文本字段(ziptext),我需要点击一个按钮(提交),将该邮政编码转换为纬度和经度坐标,然后搜索商店附近的所有位置并填充带有结果的表格视图(zipcode)。

public List<String[]> Adresses { get; set; }
public List<String[]> SpecAdresses;

public ZipController (IntPtr handle) : base (handle)
{
    Adresses = new List<String[]> ();
    SpecAdresses = new List<String[]> ();
}

public override void ViewDidLoad ()
{
    base.ViewDidLoad ();
    Zipcode.RegisterClassForCellReuse (typeof(UITableViewCell), callHistoryCellId);
    Zipcode.Source = new CallHistoryDataSource (this);      
    Submit.TouchUpInside += (object sender, EventArgs e) => {
        CLGeocoder latlng = new CLGeocoder ();
        latlng.GeocodeAddress (Ziptext.Text, HandleCLGeocodeCompletionHandler);
    };
}

void HandleCLGeocodeCompletionHandler (CLPlacemark[] placemarks, NSError error)
{
    for(int i = 0; i < placemarks.Length; i++)
    {
        lat = placemarks [i].Location.Coordinate.Latitude;
        lng = placemarks [i].Location.Coordinate.Longitude;
    }
    Console.WriteLine (lat.ToString() + ":" + lng.ToString());
}

/**
Methods to sort though the list and find stores with in close proximity
**/

class CallHistoryDataSource : UITableViewSource
{
    ZipController controller;

    public CallHistoryDataSource (ZipController controller)
    {
        this.controller = controller;
    }

    // Returns the number of rows in each section of the table
    public override int RowsInSection (UITableView tableView, int section)
    {
        return controller.SpecAdresses.Count;
    }
    //
    // Returns a table cell for the row indicated by row property of the NSIndexPath
    // This method is called multiple times to populate each row of the table.
    // The method automatically uses cells which have scrolled off the screen or creates new ones as necessary.
    //
    public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
    {
        CustomCell cell = tableView.DequeueReusableCell (ZipController.ZipId) as CustomCell;
        if (cell == null) {
            cell = new CustomCell (ZipController.ZipId);
        }

        int row = indexPath.Row;
        string[] t = controller.SpecAdresses [row];

        cell.UpdateCell (//------//);
        return cell;
    }


    public override float GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
    {
        return 80.0f;
    }
}

我已设法转换邮政编码并找到附近的所有位置。但是,我不知道如何制作它,以便每次点击按钮时都会更新表格视图。换句话说,我每次点击提交都需要表来刷新数据。

2 个答案:

答案 0 :(得分:0)

您需要更新表格源的基础数据(看起来您已经在做),然后您应该做的就是调用

TableView.ReloadData();

在你的控制器中告诉TableView它需要刷新自己

答案 1 :(得分:0)

在提交按钮的TouchUpInside事件上 - &gt;你想做以下事情:

  1. 更新数据源(为表提供“新”数据源)
  2. 在“表格”上重新加载数据(刷新表格,使其从“数据”源读取)
  3. 您在创建CallHistoryDataSource时已经将引用传递给控制器​​,因此您需要做的就是使用要显示的数据更新SpecAdreses列表并重新加载表数据。

    一些代码:

        Submit.TouchUpInside += (object sender, EventArgs e) => {
        CLGeocoder latlng = new CLGeocoder ();
        latlng.GeocodeAddress (Ziptext.Text, HandleCLGeocodeCompletionHandler);
    
         // add code here to update the SpecAdresses list with the data you want to display
    
        Zipcode.ReloadData() // refresh the table
    };