將 UIRefreshControl 新增到 TableView
假設:
TableView - 對 TableView 的引用
DataSource - 是一個繼承 UITableViewSource 的類
DataSource.Objects - 是一個公共 List <object>(),可供 UIViewController 訪問
private UIRefreshControl refreshControl;
public override void ViewDidLoad()
{
base.ViewDidLoad();
// Set the DataSource for the TableView
TableView.Source = dataSource = new DataSource(this);
// Create the UIRefreshControl
refreshControl = new UIRefreshControl();
// Handle the pullDownToRefresh event
refreshControl.ValueChanged += refreshTable;
// Add the UIRefreshControl to the TableView
TableView.AddSubview(refreshControl);
}
private void refreshTable(object sender, EventArgs e)
{
fetchData();
refreshControl.EndRefreshing();
TableView.ReloadData();
}
private void fetchData()
{
var objects = new List<object>();
// fetch data and store in objects.
dataSource.Objects = objects;
}