4

我正在将之前使用 UITableView 的屏幕重写为 UICollectionView。但是我在单元格内的按钮上的单击处理程序遇到问题。

collectionView.RegisterNibForCell (DocumentViewCell.Nib, docCellId);    
...

public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    var docCell = (DocumentViewCell)collectionView.DequeueReusableCell (docCellId, indexPath);                
    docCell.BtnDelete.Hidden = !EditMode;
    docCell.BtnDelete.TouchUpInside += delegate(object sender, EventArgs e) {
        Logging.Debug("Crashes before if reaches here");
    };
    return docCell;
}

我知道该单元格已开始重用,并且在发生这种情况时这很可能不起作用,但现在当按下删除按钮时,它会立即崩溃,列表中只有一个元素。一切都很好,直到我按下按钮然后我得到下面的堆栈跟踪。根据我几乎相同的 UITableView 代码,我认为没有理由发生这种情况。

有没有人使用基于 Nib 的 CollectionView 单元完成此操作?非常感谢任何帮助!

堆栈跟踪:

 at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication.UIApplicationMain (int,string[],intptr,intptr) <IL 0x0009f, 0xffffffff>
 at MonoTouch.UIKit.UIApplication.Main (string[],string,string) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
 at SalesApp.Application.Main (string[]) [0x00000] in /MyApp/Main.cs:18
 at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

0   SalesApp                            0x0009a85c mono_handle_native_sigsegv + 284
1   SalesApp                            0x0000e138 mono_sigsegv_signal_handler + 248
2   libsystem_c.dylib                   0x990a78cb _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01990258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
5   UIKit                               0x01a51021 -[UIControl sendAction:to:forEvent:] + 66
6   UIKit                               0x01a5157f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 578
7   UIKit                               0x01a506e8 -[UIControl touchesEnded:withEvent:] + 546
8   UIKit                               0x01c541d3 _UIGestureRecognizerUpdate + 7407
9   CoreFoundation                      0x03ecbafe __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 30

更新

此代码工作正常

public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    var docCell = (DocumentViewCell)collectionView.DequeueReusableCell (DocumentViewCell.Key, indexPath);
    docCell.BtnDelete.Hidden = !EditMode;
    docCell.BtnDelete.TouchUpInside -= HandleTouchUpInside;
    docCell.BtnDelete.TouchUpInside += HandleTouchUpInside;
    return docCell;
}

void HandleTouchUpInside (object sender, EventArgs e)
{
    Logging.Debug("No crash");
}
4

2 回答 2

4

问题是没有对您从方法docCell返回的实例的托管引用。GetCell这意味着 GC 可以随时收集它。

当再次需要它时,它将重新出现在托管世界中(使用IntPtr构造函数)。它将是相同的(重复使用的)本机实例,但是是一个新的 托管实例。发生崩溃是因为事件指向旧的托管实例(已收集)。

简单的解决方案是在视图存在时保留已创建单元的缓存。这将确保 GC 在使用之前不会收集(托管)单元格。有几个答案显示了这一点UITableView

注意:我认为我们在最新版本的 Xamarin.iOS 中修复(隐藏)了此问题。也许只是为了UITableView!?!需要检查这个...

于 2013-05-31T18:28:28.050 回答
1

我猜DequeueReusableCell返回null,因为它是第一个被绘制的单元格。

您的代码应如下所示:

public override UICollectionViewCell GetCell (UICollectionView collectionView, MonoTouch.Foundation.NSIndexPath indexPath)
{
    var docCell = (DocumentViewCell)collectionView.DequeueReusableCell (docCellId, indexPath);
    if (docCell == null)
        docCell = new UICollectionViewCell (...);
    docCell.BtnDelete.TouchUpInside += delegate(object sender, EventArgs e) {
        Logging.Debug("Crashes before if reaches here");
    };
    return docCell;
}

(...)用任何创建你的单元格的东西替换椭圆。

于 2013-05-31T14:04:40.020 回答