簡體   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