如何在Xamarin iOS中更改UITableView中的行顺序
问题描述:
我有名为“myplaces”的List,名称,图像,坐标和距离。 我用viewdidload方法中的表视图绑定了我的列表。我也使用Pull刷新控件来更新我的表格视图,但我只能更新单元格,例如图像,名称等。我想通过Distance属性更改行顺序,以便在表格视图中首先获取最近的位置。我正在更新我的列表顺序,如下所示。请帮帮我。如何在Xamarin iOS中更改UITableView中的行顺序
myPlaces = myPlaces.OrderBy(x => x.Distance).ToList();
这里是代码
UITableView table;
RrefreshTableSource tableSource;
bool useRefreshControl = false;
UIRefreshControl RefreshControl;
double PlaceRadius = 100;
public Places selectedplace;
AppDelegate appDelegate = UIApplication.SharedApplication.Delegate as AppDelegate;
public static LocationManager Manager { get; set; }
public PlacesViewController(IntPtr handle) : base (handle)
{
if (appDelegate.myPlaces.Count == 0)
{
Places p = new Places();
appDelegate.myPlaces = p.LoadPlaces();
}
Manager = new LocationManager();
Manager.StartLocationUpdates();
}
public override async void ViewDidLoad()
{
base.ViewDidLoad();
UIApplication.Notifications.ObserveDidBecomeActive((sender, args) =>
{
Manager.LocationUpdated += HandleLocationChanged;
});
UIApplication.Notifications.ObserveDidEnterBackground((sender, args) =>
{
Manager.LocationUpdated -= HandleLocationChanged;
});
Title = "Places";
table = new UITableView(new CGRect(0, 20, View.Bounds.Width, View.Bounds.Height - 20));
tableSource = new RrefreshTableSource(appDelegate.myPlaces,this);
table.Source = tableSource;
table.RowHeight = 80;
await RefreshAsync();
AddRefreshControl();
Add(table);
table.Add(RefreshControl);
}
public override void PrepareForSegue(UIStoryboardSegue segue, NSObject sender)
{
//var indexPath = table.IndexPathForSelectedRow;
var indexPath = (NSIndexPath)sender; // this was the selected row
if (segue.Identifier == "detailsview")
{
var placedetail = segue.DestinationViewController as PlacesDetailViewController;
placedetail.selectedplace = appDelegate.myPlaces[indexPath.Row];
}
}
public void HandleLocationChanged(object sender, LocationUpdatedEventArgs e)
{
// Handle foreground updates
CLLocation location = e.Location;
Manager.currentLocation = location;
//if (Manager.point != null)
//{
// double curdistance = location.DistanceFrom(Manager.point);
// lblfarfrom.Text = "Far from " + Math.Round((curdistance), 2).ToString() + " meters";
//}
CalcNearLocation(Manager.currentLocation);
//Manager.pointregionname = tbMessage.Text;
//Console.WriteLine("foreground updated");
}
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
}
async Task RefreshAsync()
{
if (useRefreshControl)
RefreshControl.BeginRefreshing();
if (useRefreshControl)
RefreshControl.EndRefreshing();
table.ReloadData();
}
#region * iOS Specific Code
void AddRefreshControl()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
{
RefreshControl = new UIRefreshControl();
RefreshControl.ValueChanged += async (sender, e) =>
{
CalcNearLocation(Manager.currentLocation);
await RefreshAsync();
};
useRefreshControl = true;
}
}
public void CalcNearLocation(CLLocation currentLocation)
{
if (appDelegate.myPlaces != null && appDelegate.myPlaces.Count > 0)
{
CLLocation closestLocation = null;
double smallestDistance = double.MaxValue;
string placename = string.Empty;
foreach (Places place in appDelegate.myPlaces)
{
CLLocation location = new CLLocation(place.points.Latitude, place.points.Longitude);
double distance = currentLocation.DistanceFrom(location);
place.Distance = ConvertMetersToKiloMeters(distance);
if (distance < smallestDistance)
{
smallestDistance = distance;
closestLocation = location;
placename = place.Name;
}
}
Console.WriteLine("Closest Location " + placename);
Manager.pointregion = new CLCircularRegion(closestLocation.Coordinate, PlaceRadius, placename);
Manager.pointregionname = placename;
appDelegate.myPlaces = appDelegate.myPlaces.OrderBy(x => x.Distance).ToList();
}
}
答
您在appDelegate.myPlaces
更新全局变量的项目,但你没有更新属于TableView中的源里面的物品。
你需要做这样的事情在你的RefreshAsync
方法
async Task RefreshAsync()
{
if (useRefreshControl)
RefreshControl.BeginRefreshing();
if (useRefreshControl)
RefreshControl.EndRefreshing();
// Update the items inside the source , then reload data
((RrefreshTableSource)table.Source).Items = appDelegate.myPlaces;
table.ReloadData();
}
如果Items
(或者是在源项目变量的名字)是不公开的,你需要将其暴露在
你在排序后调用ReloadData方法吗?你能展示更多的代码吗? – iamIcarus
@iamIcarus用代码更新了问题。 –