【问题标题】:Xamarin.iOS UITableView, how do you force a cell to update?Xamarin.iOS UITableView,如何强制单元格更新?
【发布时间】:2013-08-24 05:45:01
【问题描述】:

我正在使用 Xamarin.iOS,但无法弄清楚如何更新单个单元格。在 WPF ListView 中,我可以只做一个绑定,让单元格的属性做一个 inotifypropertychanged,它会通过绑定自动发生。 Xamarin.iOS 中是否有一些等效的功能?更新 UITableView 单元格而不只是擦除它们并重新添加它们似乎超级麻烦..

更新单个单元格的最佳方法是什么?

【问题讨论】:

    标签: ios uitableview xamarin.ios xamarin


    【解决方案1】:

    假设您的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);
    

    【讨论】:

      【解决方案2】:

      创建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 中创建自定义单元格的返回实例。

      【讨论】:

      • 这是一个非常繁琐的解决方案,更不用说视图(单元格)正在保存它显示的数据。它使事情复杂化了很多,尤其是考虑到应该重用单元格实例来显示其他数据这一事实。
      • 我不同意;我认为这是一个很好的独立解决方案,它将单元格的所有逻辑都封装在自己的类中。它允许单元负责更新自身,而不是表格。没有什么可以阻止单元实例被重用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      相关资源
      最近更新 更多