【发布时间】:2011-02-01 10:05:21
【问题描述】:
我有一个 UITableViewSource 子类。我正在覆盖 GetCell 并使用我自己的子类单元格,如下所示:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
MarketItem item=_tableItems[indexPath.Section].Items[indexPath.Row];
MarketCell cell=tableView.DequeueReusableCell(_cellIdentifier) as MarketCell;
if (cell==null)
{
cell=new MarketCell(UITableViewCellStyle.Subtitle,_cellIdentifier,item);
}
// decorate the cell
// ...
return cell;
}
这可行,但是当我在 UITableViewDelegate 中获取事件时,索引路径会获取错误的单元格(AccessoryButtonTapped、WillSelectRow 等事件)。
Section 和 Row 编号看起来是正确的,但是当我这样做时
tableView.CellAt(indexPath)
我弄错了单元格。 (行号和节号再次看起来正确。)
注意事项:
- 表不断更新 - 项目到达不同的线程,然后 InvokeOnMainThread'd
- 虽然表格不断更新,但仅添加行和部分 - 没有重新排序或删除任何内容
- 如果我在收到“WillSelectRow”时暂停更新,则无济于事
- 最有趣的是(但不是可交付的解决方案),如果我每次都创建一个新单元而不是执行 DequeueReusableCell,它可以正常工作。
我不禁认为这是我自己制造的一个愚蠢的错误,但找不到它。任何帮助将不胜感激!
【问题讨论】:
-
每次更新表数据时是否调用table.ReloadData()?
-
仅当添加新行时 - 对于现有单元格的更新,我正在执行 ReloadRows()
-
可能与信元排队有关。您的 MarketCell 构造函数如何将 _cellIdentifier 分配给新创建的单元格?
-
谢谢@riha,构造函数是:
public MarketCell(UITableViewCellStyle style, string identifier, MarketItem item) : base(style,identifier) { Item=item; } -
(抱歉格式化,无法在 cmets 中解决。)
标签: c# iphone ios xamarin.ios