使用Xamarin.iOS在单击处理程序中添加UICollectionViewCell中的按钮

使用Xamarin.iOS在单击处理程序中添加UICollectionViewCell中的按钮,第1张

概述我正在重写一个使用UITableView的屏幕到UICollectionView.但我在单元格内的按钮上遇到单击处理程序时遇到问题. collectionView.RegisterNibForCell (DocumentViewCell.Nib, docCellId); ...public override UICollectionViewCell GetCell (UICollect 我正在重写一个使用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) <IL 0x00050,0xffffffff>Native stacktrace:0   SalesApp                            0x0009a85c mono_handle_native_sigsegv + 2841   SalesApp                            0x0000e138 mono_sigsegv_signal_handler + 2482   libsystem_c.dylib                   0x990a78cb _sigtramp + 433   ???                                 0xffffffff 0x0 + 42949672954   UIKit                               0x01990258 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 615   UIKit                               0x01a51021 -[UIControl sendAction:to:forEvent:] + 666   UIKit                               0x01a5157f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 5787   UIKit                               0x01a506e8 -[UIControl touchesEnded:withEvent:] + 5468   UIKit                               0x01c541d3 _UIGestureRecognizerUpdate + 74079   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");}
解决方法 问题是没有对从GetCell方法返回的docCell实例的托管引用.这意味着GC可以随时收集它.

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

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

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

总结

以上是内存溢出为你收集整理的使用Xamarin.iOS在单击处理程序中添加UICollectionViewCell中的按钮全部内容,希望文章能够帮你解决使用Xamarin.iOS在单击处理程序中添加UICollectionViewCell中的按钮所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1054090.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存