Xamarin.iOS UITableView,你如何强制一个单元格更新?
我正在使用Xamarin.iOS,并不知道如何更新单个单元格。 在WPF ListView中,我可以做一个绑定,并让单元格的属性做一个inotifypropertychanged,它自动通过绑定发生。 Xamarin.iOS中有一些等效的function吗? 更新UITableView单元格而不删除它们并重新添加它们似乎是非常麻烦的。
什么是更新个别单元格的最佳方法?
假设你的UITableView
存储在一个“tableView”variables中:
NSIndexPath[] rowsToReload = new NSIndexPath[] { NSIndexPath.FromRowSection(1, 0) // points to second row in the first section of the model }; tableView.ReloadRows(rowsToReload, UITableViewCellRowAnimation.None);
创build一个UICell
的子类,并在那里绑定到INotifyPropertyChanged
。
您可以为UICell
正在显示的模型对象创build一个公共属性。
然后在模型更改或属性更改时更新单元格的显示属性…
public class CarCell : UITableViewCell { private Car car; public Car Car { get { return this.car; } set { if (this.car == value) { return; } if (this.car != null) { this.car.PropertyChanged -= HandlePropertyChanged; } this.car = value; this.car.PropertyChanged += HandlePropertyChanged; SetupCell(); } } private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) { SetupCell(); } private void SetupCell() { this.TextLabel.Text = this.car.Title; } }
然后,您需要在UITableViewDataSource
创build自定义单元格的返回实例。