English 中文(简体)
为什么我的UISwitch是tableView.cell.AccessoryView时会崩溃
原标题:Why does my UISwitch crash when it is a tableView.cell.AccessoryView

使用MonoTouch时出现此问题。

I m trying to have a TableView with cells that have a UISwitch, but the app crashes when the switch is clicked. Just for testing, I also created a switch that was merely appended as subview of the TableView itself. The only difference between both switches is their parent. The one with th TableView parent works, the one that is in a cell as its AccessoryView crashes when clicked.

For testing, I created a fresh iPhone navigation based application (DynUISwitch). I only edited the generated RootViewController.xib.cs as follows:

  using MonoTouch.UIKit;
  using System;
  using System.Drawing;

  using MonoTouch.Foundation;

  namespace DynUISwitch
  {
    partial class RootViewController : UITableViewController
    {
      public RootViewController (IntPtr handle) : base(handle)
      {
      }

      public override void ViewDidLoad ()
      {
        base.ViewDidLoad ();
        RectangleF frame = new RectangleF (100f, 100f, 20f, 20f);
        UISwitch uiSwitch = new UISwitch (frame);
        uiSwitch.ValueChanged += delegate {
          Console.WriteLine ("View Switch value now is {0}", uiSwitch.On);
        };

       this.View.AddSubview (uiSwitch);
        this.TableView.Source = new DataSource (this);
      }
      class DataSource : UITableViewSource
      {
        RootViewController controller;

        public DataSource (RootViewController controller)
        {
          this.controller = controller;
        }
        public override int NumberOfSections (UITableView tableView)
        {
          return 1;
        }
        public override int RowsInSection (UITableView tableview, int section)
        {
          return 1;
        }
        public override UITableViewCell GetCell (UITableView tableView, MonoTouch.Foundation.NSIndexPath indexPath)
        {
          string cellIdentifier = "Cell";
          var cell = tableView.DequeueReusableCell (cellIdentifier);
          if ( cell == null )
          {
            cell = new UITableViewCell (UITableViewCellStyle.Default, cellIdentifier);
          }
          RectangleF frame = new RectangleF (0f, 0f, 20f, 20f);
          UISwitch uiSwitch = new UISwitch (frame);
          uiSwitch.ValueChanged += delegate {
            Console.WriteLine ("Cell Switch value is now {0}", uiSwitch.On);
          };
          cell.AccessoryView = uiSwitch;
          return cell;
        }
      }
    }
  }

When running the app, I click on the center UISwitch, the one directly attached to the TableView. The application output window shows that the delegate receives and processes the click. I then click the UiSwitch that is the cell.AccessoryView and I Immediately get following crash:

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) [0x00038] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:26
at MonoTouch.UIKit.UIApplication.Main (string[]) [0x00000] in /Users/plasma/Source/iphone/monotouch/UIKit/UIApplication.cs:31
at DynUISwitch.Application.Main (string[]) [0x00000] in /Users/guivho/Mono/Learning/DynUISwitch/Main.cs:14
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>

本地堆栈种族:

0   DynUISwitch                         0x000d1965 mono_handle_native_sigsegv + 343
1   DynUISwitch                         0x0000ffb4 mono_sigsegv_signal_handler + 322
2   libSystem.B.dylib                   0x9666e45b _sigtramp + 43
3   ???                                 0xffffffff 0x0 + 4294967295
4   UIKit                               0x01c981b5 -[UIControl sendAction:to:forEvent:] + 67
5   UIKit                               0x01c9a647 -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
6   UIKit                               0x01ddcc6d -[UISwitch _onAnimationDidStop:finished:context:] + 201
7   UIKit                               0x01c3a665 -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 294
8   UIKit                               0x01c3a4f7 -[UIViewAnimationState animationDidStop:finished:] + 77
9   QuartzCore                          0x030756cb _ZL23run_animation_callbacksdPv + 278
10  QuartzCore                          0x03075589 _ZN2CAL14timer_callbackEP16__CFRunLoopTimerPv + 157
11  CoreFoundation                      0x00e20fe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
12  CoreFoundation                      0x00e22594 __CFRunLoopDoTimer + 1220
13  CoreFoundation                      0x00d7ecc9 __CFRunLoopRun + 1817
14  CoreFoundation                      0x00d7e240 CFRunLoopRunSpecific + 208
15  CoreFoundation                      0x00d7e161 CFRunLoopRunInMode + 97
16  GraphicsServices                    0x03ede268 GSEventRunModal + 217
17  GraphicsServices                    0x03ede32d GSEventRun + 115
18  UIKit                               0x01c1842e UIApplicationMain + 1160
19  ???                                 0x07fe0744 0x0 + 134088516
20  ???                                 0x07fe067a 0x0 + 134088314
21  ???                                 0x07ef2b96 0x0 + 133114774
22  ???                                 0x07ef2ae2 0x0 + 133114594
23  ???                                 0x07ef2b6d 0x0 + 133114733
24  DynUISwitch                         0x0000fd6f mono_jit_runtime_invoke + 1332
25  DynUISwitch                         0x001ee239 mono_runtime_invoke + 137
26  DynUISwitch                         0x001f0920 mono_runtime_exec_main + 669
27  DynUISwitch                         0x001efd0a mono_runtime_run_main + 843
28  DynUISwitch                         0x000a3c62 mError connecting stdout and stderr (127.0.0.1:10001)
ono_jit_exec + 200
29  DynUISwitch                         0x002a25eb main + 3838
30  DynUISwitch                         0x000030c9 _start + 208
31  DynUISwitch                         0x00002ff8 start + 40
32  ???                                 0x00000002 0x0 + 2

来自gdb的调试信息:

/tmp/mono-gdb-commands.nzt3EM:1: Error in sourced command file:
unable to debug self

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

I ve done this kind of thing in an Objective-C / XCode app (having a UISwitch as AccessoryView for a TableCell).

有什么建议为什么它在上面的演示应用程序中不起作用?

最佳回答

尝试声明

UISwitch uiSwitch;

在班级层面。当你的方法超出范围时,GC可能会清理你的uiSwitch引用,这样当委托激发它时,它的引用就不在了。许多人从MT 4.0开始就遇到过类似的问题。

问题回答

注意:在您的代码中,每次使用单元时都会添加一个新的UISwitch,而不仅仅是在创建单元时。在该列表上滚动几十个项目可能足以使应用程序因堆栈溢出而崩溃。

要解决此问题,请确保将UISwitch的创建移到if(cell==null)语句中,这样UISwitch只在创建新单元格时创建,而不是在从缓存中重用时创建。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签