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

时间:2013-06-25 06:20:28

标签: iphone uitableview xamarin.ios uitapgesturerecognizer

我在UITapGestureRecognizer中使用UIImageView UITableViewCell。它显示所有单元格,但当我试图点击图像它崩溃。无法弄清楚解决方案。我提到了下面的代码。

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.
=================================================================

2 个答案:

答案 0 :(得分:0)

我发现问题是GetCell调用新UITapGestureRecognizer正在创建并添加一个目标。将该块移到if语句中,以便在创建UITableViewCell时运行它。

此外,我建议您使用点击事件功能(使用UIImageView)创建UITapGestureRecognizer子类,以避免在需要该事件的任何地方添加它。

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

答案 1 :(得分:0)

您的崩溃可能是因为没有保留对

的引用

UIImageView toCheckboximage;

UIView toCheckImgV

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