我正在使用Xamarin.iOS,无法弄清楚如何更新单个单元格。在WPF ListView中,我可以只进行绑定并让单元格的属性执行inotifypropertychanged并且它会通过绑定自动发生。 Xamarin.iOS中是否有一些等效的功能?更新UITableView单元似乎超级麻烦,而不是擦除它们并重新添加它们。
更新单个细胞的最佳方法是什么?
答案 0 :(得分:18)
假设您的UITableView
存储在“tableView”变量中:
NSIndexPath[] rowsToReload = new NSIndexPath[] {
NSIndexPath.FromRowSection(1, 0) // points to second row in the first section of the model
};
tableView.ReloadRows(rowsToReload, UITableViewCellRowAnimation.None);
答案 1 :(得分:3)
创建UICell
的子类并绑定到INotifyPropertyChanged
。
您可以为UICell
正在显示的模型对象创建公共属性。
然后在模型更改时或属性更改时更新单元格的显示属性...
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
。