所以在我的应用程序中,我使用带有自定义UITableViewCell的UITableView来显示' CustomItems'包含UISegmentedControl作为On / Off开关。更改单元格2中UISegmentedControl的值,应打开/关闭CustomItem [2]等。
当工作线程中的值发生更改时,UISegmentedControl会对CustomItem执行操作。我有99%的时间都能正常使用这段代码 - 实际上我从来没有看到它行为不端。但是,我们有客户报告UISegemented控件有时无法控制正确的项目。
在我的UITableViewSource' GetCell()'方法,我有类似的代码:
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell(cellIdentifier) as CustomCell ?? new CustomCell(cellIdentifier);
. . .
// Handle the UISegementedControl (On/Off switch) value changed event
cell.segmentControl.ValueChanged += delegate
{
// Do some stuff
cell.outputControl.TintColor = cell.outputControl.SelectedSegment == 0 ? UIColor.Gray : UIColor.Red;
var currentItem = tableItems[cell.IndexPath.Row];
TurnSomethingOnOrOff(currentItem);
}
}
private void TurnSomethingOnOrOff(CustomItem currentItem)
{
var worker = new BackgroundWorker();
worker.DoWork += (sender, args) => {
// turn currentItem on or off
};
worker.RunWorkerAsync ();
}
用户看到的是,有时更改单元格1中UISegementedControl的值,正在打开或关闭CustomItem [0]和CustomItem [1]。
我不确定如何让我的应用进入我的客户所能达到的状态(他们也不完全确定,似乎随机发生)。
这可能与垃圾收集或其他原因有关,为什么我的单元格会丢失正确的事件处理程序?
该列表中只有2或3个项目,因此单元格通常不会被重复使用。我测试了单元格重用(通过滚动屏幕以便必须重新显示单元格),所有发生的事情是ValueChanged事件被触发两次,但错误的事件从未被触发过。