繁体   English   中英

在uiableview单元格Montouch中点击手势识别器

[英]Tap Gesture Recognizer in uitableview cell Montouch

我在UITableViewCellUIImageView使用UITapGestureRecognizer 它显示了所有单元格,但是当我尝试点击“图像”时,它崩溃了。 无法找出解决方案。 我在下面提到了代码。

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
string cellName = "Cell";
UITableViewCell cell;
cell = tableView.DequeueReusableCell ("cellIdentifier");
if (cell == null) {
    cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
    UIImageView toCheckboximage;
    toCheckboximage = new UIImageView (new RectangleF (20, 10, 20, 20)); 
    toCheckboximage.UserInteractionEnabled = true;
    toCheckboximage.Tag = 1;
    cell.ContentView.AddSubview (toCheckboximage);
}
UIView toCheckImgV = (UIImageView)cell.ContentView.ViewWithTag (1);
cell.SelectionStyle = UITableViewCellSelectionStyle.None;

tapRecognizer = new UITapGestureRecognizer ();
toCheckImgV.AddGestureRecognizer (tapRecognizer);
tapRecognizer.AddTarget (() => {
    Console.WriteLine("coming");
});
  return cell;
}

提前致谢

更新:在日志中我得到以下错误

Stacktrace:

  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 CyramedX.UI.Application.Main (string[]) [0x00016] in /Users/qatalys/Desktop/monotouch/Jun_24_2013/CyramedX/CyramedX.UI/Main.cs:21
  at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

Native stacktrace:

    0   CyramedXUI                          0x0007cc9c mono_handle_native_sigsegv + 284
    1   CyramedXUI                          0x00004fe8 mono_sigsegv_signal_handler + 248
    2   libsystem_c.dylib                   0x970308cb _sigtramp + 43
    3   ???                                 0xffffffff 0x0 + 4294967295
    4   UIKit                               0x014d499b -[UIGestureRecognizer _updateGestureWithEvent:] + 333
    5   UIKit                               0x014d60df -[UIGestureRecognizer _delayedUpdateGesture] + 46
    6   UIKit                               0x014d8d2d ___UIGestureRecognizerUpdate_block_invoke_0543 + 57
    7   UIKit                               0x014d8cac _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 331
    8   UIKit                               0x014d0a28 _UIGestureRecognizerUpdate + 1348
    9   UIKit                               0x0123d972 -[UIWindow _sendGesturesForEvent:] + 1283
    10  UIKit                               0x0123de53 -[UIWindow sendEvent:] + 98
    11  UIKit                               0x0121bd4a -[UIApplication sendEvent:] + 436
    12  UIKit                               0x0120d698 _UIApplicationHandleEvent + 9874
    13  GraphicsServices                    0x04a58df9 _PurpleEventCallback + 339
    14  GraphicsServices                    0x04a58ad0 PurpleEventCallback + 46
    15  CoreFoundation                      0x03e21bf5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    16  CoreFoundation                      0x03e21962 __CFRunLoopDoSource1 + 146
    17  CoreFoundation                      0x03e52bb6 __CFRunLoopRun + 2118
    18  CoreFoundation                      0x03e51f44 CFRunLoopRunSpecific + 276
    19  CoreFoundation                      0x03e51e1b CFRunLoopRunInMode + 123
    20  GraphicsServices                    0x04a577e3 GSEventRunModal + 88
    21  GraphicsServices                    0x04a57668 GSEventRun + 104
    22  UIKit                               0x0120affc UIApplicationMain + 1211
    23  ???                                 0x0ed8effd 0x0 + 249098237
    24  ???                                 0x0ed8cea0 0x0 + 249089696
    25  ???                                 0x0ed8c4cc 0x0 + 249087180
    26  ???                                 0x0ed8c636 0x0 + 249087542
    27  CyramedXUI                          0x00009212 mono_jit_runtime_invoke + 722
    28  CyramedXUI                          0x00150bee mono_runtime_invoke + 126
    29  CyramedXUI                          0x00154d14 mono_runtime_exec_main + 420
    30  CyramedXUI                          0x00159845 mono_runtime_run_main + 725
    31  CyramedXUI                          0x00065ea5 mono_jit_exec + 149
    32  CyramedXUI                          0x001dd094 main + 1988
    33  CyramedXUI                          0x00002b55 start + 53

=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries 
used by your application.
=================================================================

我发现一个问题,在任何GetCell调用中,新的UITapGestureRecognizer都在创建并添加另一个目标。 在创建UITableViewCell时,将该块移到if语句中运行一次。

此外,我建议您使用点击事件功能(使用UITapGestureRecognizer )创建UIImageView子类,以避免将其添加到需要该事件的任何位置。

请从Xamarin Studio的控制台添加Application Log数据。 没有这些信息,就很难理解确切的问题在哪里。

您的当机原因可能是由于未保留对

UIImageView toCheckboximage;

要么

UIView toCheckImgV

您应该创建一个UITableViewCell子类,并将UIImageView作为属性添加到该类,以便在C#中保留引用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM