【问题标题】:Exception when creating a cell in Uitablesource getcell method in xamarin ios在 xamarin ios 中的 Uitablesource getcell 方法中创建单元格时出现异常
【发布时间】:2016-09-22 20:31:35
【问题描述】:

我正在使用 google places api 来提供建议的搜索。因此它会为每个命中的字符搜索 api。我有一个更新的表源。 这是GetCell方法

public override UITableViewCell GetCell(UITableView tableView,NSIndexPathindexPath)
{
  var cell = base.GetCell(tableView, indexPath) as CityCell;
  cell.SubscriteToCheckEvent(() =>
{
    var ddd = cell as CityCell;
    viewModel.ZipCode = cell.CityNameText.Text;
    viewModel.SearchStoresCommand.Execute(true);
 });
   return cell;

 }

当我尝试将值更新为快速时出现异常

Index was out of range. Must be non-negative and less than the size of the collection.

有什么办法可以避免这个异常?

任何建议都会有所帮助。

【问题讨论】:

  • 什么是基类?通常,您不会调用base.GetCell,除非您的基础是您创建的另一个类。如果您不是从自己的类继承,那么您应该调用tableView.DequeueReusableCell 来获取单元格。

标签: c# ios xamarin xamarin.ios mvvmcross


【解决方案1】:

您调用“GetCell”方法的方式不正确。

这是 GetCell 的示例:

public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            MyTableCell cell = tableView.DequeueReusableCell (CellID) as MyTableCell;
            if (null == cell) {
                //Init custom cell
                cell = new MyTableCell (UITableViewCellStyle.Default, CellID);
                //Bind delete event
                cell.CellClicked += delegate {
                    //Do something for click event
                };
            }
            return cell;
        }

这是我为其他人写的关于UITableView数据管理的完整示例,你应该看看:

UITableViewDataManagementSample

您需要分享更多代码,否则我们无法帮助您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2015-02-17
    • 1970-01-01
    • 2018-12-15
    相关资源
    最近更新 更多