如何在Xamarin iOS中更改UITableView中的行顺序
我有一张名为“我的地方”的名单,上面有名字、图像、坐标和距离。 我在viewdidload方法中将列表与表视图绑定。我还使用Pull to refresh控件来更新我的表视图,但我只能更新单元格,例如图像、名称等。我想通过Distance属性更改行顺序,以首先在表视图中获得最近的位置。我正在更新我的列表顺序,如下所示。请帮帮我如何在Xamarin iOS中更改UITableView中的行顺序,ios,xamarin,xamarin.ios,xamarin.forms,Ios,Xamarin,Xamarin.ios,Xamarin.forms,我有一张名为“我的地方”的名单,上面有名字、图像、坐标和距离。 我在viewdidload方法中将列表与表视图绑定。我还使用Pull to refresh控件来更新我的表视图,但我只能更新单元格,例如图像、名称等。我想通过Distance属性更改行顺序,以首先在表视图中获得最近的位置。我正在更新我的列表顺序,如下所示。请帮帮我 myPlaces = myPlaces.OrderBy(x => x.Distance).ToList(); 这是密码 UITableView table;
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();
}
}
UITableView表;
表源表源;
bool useRefreshControl=false;
UIRefreshControl刷新控制;
双半径=100;
公共场所选择地点;
AppDelegate AppDelegate=UIApplication.SharedApplication.Delegate作为AppDelegate;
公共静态位置管理器管理器{get;set;}
公共场所可视控制器(IntPtr句柄):基础(句柄)
{
if(appDelegate.myPlaces.Count==0)
{
地点p=新地点();
appDelegate.myPlaces=p.LoadPlaces();
}
Manager=新位置管理器();
Manager.StartLocationUpdates();
}
公共重写异步void ViewDidLoad()
{
base.ViewDidLoad();
UIApplication.Notifications.ObservedBecomeActive((发送方,参数)=>
{
Manager.LocationUpdated+=手动位置已更改;
});
UIApplication.Notifications.ObservedenterBackground((发送方,参数)=>
{
Manager.LocationUpdated-=手动位置已更改;
});
Title=“地点”;
表=新的UITableView(新的CGRect(0,20,View.Bounds.Width,View.Bounds.Height-20));
tableSource=new rrefreshttableSource(appDelegate.myPlaces,this);
table.Source=tableSource;
表1.RowHeight=80;
等待刷新异步();
AddRefreshControl();
增加(表);
表.添加(刷新控制);
}
public override void PrepareForSegue(UIStoryboardSegue,NSObject发送方)
{
//var indexPath=table.IndexPathForSelectedRow;
var indepath=(nsindepath)sender;//这是所选行
if(segue.Identifier==“detailsview”)
{
var placedetail=segue.DestinationViewController作为placeDetailViewController;
placedetail.selectedplace=appDelegate.myPlaces[indexPath.Row];
}
}
public void HandleLocationChanged(对象发送方,位置更新目标)
{
//处理前台更新
CLLocation location=e.位置;
Manager.currentLocation=位置;
//如果(Manager.point!=null)
//{
//double curdistance=位置.DistanceFrom(Manager.point);
//lblfarfrom.Text=“远离”+Math.Round((curdistance),2.ToString()+“米”;
//}
CalcNearLocation(经理当前位置);
//Manager.pointregionname=tbMessage.Text;
//控制台写入线(“前台更新”);
}
公共覆盖无效视图将出现(布尔动画)
{
基本视图将显示(动画);
}
公共覆盖无效视图显示(布尔动画)
{
基本视图显示(动画);
}
异步任务刷新异步()
{
if(useRefreshControl)
RefreshControl.BeginRefreshing();
if(useRefreshControl)
EndRefreshing();
table.ReloadData();
}
#区域*iOS特定代码
void AddRefreshControl()
{
if(UIDevice.CurrentDevice.CheckSystemVersion(6,0))
{
RefreshControl=新UIRefreshControl();
RefreshControl.ValueChanged+=async(发送方,e)=>
{
CalcNearLocation(经理当前位置);
等待刷新异步();
};
useRefreshControl=true;
}
}
公共无效CalcNearLocation(CLLocation currentLocation)
{
if(appDelegate.myPlaces!=null&&appDelegate.myPlaces.Count>0)
{
CLLocation closestLocation=null;
double smallestDistance=double.MaxValue;
string placename=string.Empty;
foreach(appDelegate.myPlaces中的位置)
{
CLLocation location=新的CLLocation(地点.点.纬度,地点.点.经度);
双距离=当前位置。距离(位置);
地点.距离=转换器仪表Tokilometers(距离);
if(距离<最小距离)
{
最小距离=距离;
闭合位置=位置;
placename=place.Name;
}
}
Console.WriteLine(“最近位置”+地名);
Manager.pointregion=new CLCircularRegion(closestLocation.Coordinate、PlaceRadius、placename);
Manager.pointregionname=地名;
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();
}
如果
项
(或源中项的变量名称)不是公共的,则需要公开它排序后是否调用ReloadData方法?你能显示更多的代码吗?@iamIcarus用代码更新了问题。