2012-07-11 4 views
1

현재 UISwitches가 포함 된 사용자 정의 UITableViewCell (XCode로 설계된)이있는 UITableView가있는 Monotouch 프레임 워크로 IOS 용 응용 프로그램을 만들고 있습니다.Monotouch UISwitch.valueChanged 대리인 대상 오류

은 내가 UITableViewSource을 통해 세포를 관리하지만 내 세포의 스위치 UISwitch.ValueChanged 이벤트에 assignated 위임 방법에 대한 콜백을 관리하면서 나는 분명히 문제가있다.

 // This class manages the TableView content and its rows 
    class MyTableViewDelegate : UITableViewSource 
    { 
     SatellitesViewController  satController; 

     List<long>      allocatedCells; 

     public MyTableViewDelegate (SatellitesViewController controller) 
     { 
      satController = controller; 
      allocatedCells = new List<long>(); 
     } 

     public override int  RowsInSection (UITableView tableview, int section) 
     { 
      return satController.sats.Count; 
     } 

     // Called when the switch to show the satellite's display state is touched 
     public void    showButtonHandler(object sender, EventArgs e) 
     { 
      System.Console.WriteLine("SHOW BUTTONHANDLER"); 
      if (sender != null) 
      { 
       CustomSwitch switchButton = (CustomSwitch) sender; 
       TLEForRedis  satToShow = this.satController.getSatTLEs(switchButton.idSat, false); 

       if (satToShow != null) 
       { 
        if (switchButton.On == true) 
         this.satController.glView.showSatellite(satToShow); 
        else 
         this.satController.glView.hideSatellite(satToShow.Id); 
       } 
      } 
     } 

     // Called when the switch to show the satellite's display state is touched 
     public void    orbitButtonHandler(object sender, EventArgs e) 
     { 
      System.Console.WriteLine("ORBIT BUTTONHANDLER"); 
      if (sender != null) 
      { 
       CustomSwitch orbitButton = (CustomSwitch) sender; 
       TLEForRedis  satToShow = this.satController.getSatTLEs(orbitButton.idSat, false); 

       if (satToShow != null) 
        this.satController.glView.setSatelliteOrbitDisplayState(satToShow.Id, orbitButton.On); 
      } 
     } 

     // Create the cell at indexPath 
     public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath) 
     { 
      string   cellName; 
      string   nibName; 
       cellName = "PROTO_CELL"; 
       nibName = "CellView"; 
      // Get the table cell which is described in the CellView.xib interface 
      var cell = tableView.DequeueReusableCell(cellName) as CellView; 

      if (cell == null) 
      { 
       cell = new CellView(); 
       var views = NSBundle.MainBundle.LoadNib(nibName, cell, null); 
       cell = Runtime.GetNSObject(views.ValueAt(0)) as CellView; 
      } 

      // Modify the cell with its own data 
      SatsList sat = (SatsList) satController.sats [indexPath.Row]; 

      CustomSwitch showButton = (CustomSwitch)cell.ViewWithTag(1); 
      UILabel   satName = (UILabel)cell.ViewWithTag(2); 
      UILabel   catalogNumber = (UILabel)cell.ViewWithTag(3); 
      CustomSwitch favoriteButton = (CustomSwitch)cell.ViewWithTag(4); 
      CustomSwitch orbitButton = (CustomSwitch)cell.ViewWithTag(5); 

      UIColor   lightgrey = new UIColor(0.85f, 0.85f, 0.85f, 1.0f); 
      tableView.BackgroundColor = lightgrey; 

      if (sat != null) 
      { 
       satName.Text = sat.satName; 
       catalogNumber.Text = string.Format("{0}", sat.catalogNumber); 
      } 
      else 
       System.Console.WriteLine("[ERROR] Satellite was not found for row " + indexPath.Row); 

      // Set switch state to on when the satellite is already being displayed 
      if (this.satController.glView.checkForSatelliteExistency(sat.Id)) 
      { 
       System.Console.WriteLine(sat.satName + "found"); 
       showButton.SetState(true, false); 
       Satellite sat3D = this.satController.glView.checkForSatelliteOrbit(sat.Id); 
       if (sat3D.displayOrbit()) 
        orbitButton.SetState(true, false); 
      } 
      else // force switch state to off when satellite is not displayed yet 
      { 
       showButton.SetState(false, false); 
       orbitButton.SetState(false, false); 
      } 
      favoriteButton.SetState(false, false); 

      // Bind changing state switches' events in order to display or hide the corresponding satellite 
      showButton.setData(sat.Id); 
      favoriteButton.setData(sat.Id); 
      orbitButton.setData(sat.Id); 
      if (!this.allocatedCells.Contains(indexPath.Row)) 
      { 
       showButton.ValueChanged += this.showButtonHandler; 
       orbitButton.ValueChanged += this.orbitButtonHandler; 
      } 
      this.allocatedCells.Add(indexPath.Row); 
      return cell; 
     } 

}

사실, 그 위의 코드의 결과, 나는 위/아래에서 위로 스크롤 할 수 있다는 것입니다 : 여기

은 좀 더 명시 적 수 있습니다, 내가 작업하고있는 UITableViewSource 코드 문제가없는 tableView와 스크롤을 통해 이미 화면 밖으로 나가는 스위치를 클릭하지 않을 때까지 " SHOW BUTTONHANDLER"줄을 인쇄 할 수도 있습니다. , 나는 목록의 상단에 이전에 그려진하고 난 다시 위로 스크롤 한 후, 등장하는 셀의 일부인 스위치를 클릭 할 때 나는 다음과 같은 오류 얻을 :

[7864:1507] -[UIControlTargetAction BridgeSelector]: unrecognized selector sent to instance 0x14462a70 

또는 다음 스택 추적 :

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) [0x00042] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:29 
    at App.Application.Main (string[]) [0x0001b] in App/Main.cs:24 
    at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff> 

Native stacktrace: 

0 App      0x0009094c mono_handle_native_sigsegv + 284 
1 App      0x00005cd8 mono_sigsegv_signal_handler + 248 
2 libSystem.B.dylib     0x90d9c05b _sigtramp + 43 
3 ???         0xffffffff 0x0 + 4294967295 
4 UIKit        0x0222e55a -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61 
5 UIKit        0x022d3b76 -[UIControl sendAction:to:forEvent:] + 66 
6 UIKit        0x022d403f -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 503 
7 UIKit        0x02425a6c -[_UISwitchInternalView _sendActions] + 121 
8 Foundation       0x0173786d __NSFireDelayedPerform + 389 
9 CoreFoundation      0x01195966 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 22 
10 CoreFoundation      0x01195407 __CFRunLoopDoTimer + 551 
11 CoreFoundation      0x010f87c0 __CFRunLoopRun + 1888 
12 CoreFoundation      0x010f7db4 CFRunLoopRunSpecific + 212 
13 CoreFoundation      0x010f7ccb CFRunLoopRunInMode + 123 
14 GraphicsServices     0x04789879 GSEventRunModal + 207 
15 GraphicsServices     0x0478993e GSEventRun + 114 
16 UIKit        0x0222ba9b UIApplicationMain + 1175 
17 ???         0x0ebdde65 0x0 + 247324261 
18 ???         0x0ebdc550 0x0 + 247317840 
19 ???         0x0b7f7bac 0x0 + 192904108 
20 ???         0x0b7f7d86 0x0 + 192904582 
21 App      0x0000a042 mono_jit_runtime_invoke + 722 
22 App      0x00169f4e mono_runtime_invoke + 126 
23 App      0x0016e034 mono_runtime_exec_main + 420 
24 App      0x00173455 mono_runtime_run_main + 725 
25 App      0x00067245 mono_jit_exec + 149 
26 App      0x002116a5 main + 2837 
27 App      0x00003095 start + 53 
28 ???         0x00000004 0x0 + 4 

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

솔루션에 대한 아이디어가 있습니까? 스위치에 대한 대리자 메서드가 잘못 등록 된 것 같지만 무엇이 잘못되었는지는 알 수 없습니다. 또한 위임자를 한 번만 등록했지만 그 결과는 동일하므로

if (!this.allocatedCells.Contains(indexPath.Row)) 

줄을 사용했기 때문에 결과가 동일합니다. 해당 셀을 제거해야하고 셀이 화면 밖으로 나왔을 때 대리인을 "등록 취소"하려고합니까?

나는 당신이 아이디어를 가지기를 바라고, 모든 것에 대해 당신의 의견/조언/의견을 게시하기를 망설이지 않습니다.

미리 감사드립니다.

답변

1

좋아, 내 코드에서 문제가 발견되었습니다. 대리인이 스위치에 바인딩되어 있기 때문입니다. 사실, 나는 스크린 밖으로 나가고 있던 세포의 대표들을 등록하는 것을 잊었다. ReuseableCell을 dequeing한다는 사실은 셀 내용에 링크 된 델리게이트를 제거하지 않습니다. =>이 리드는 참조가 변경된 발신자와 함께 내 showButtonHandler (sender, args) 메소드를 호출합니다. 내 오래된 allocatedCells 목록에서 셀을 등록, 내 경우에는

// Bind changing state switches' events in order to display or hide the corresponding satellite 
      showButton.setData(sat.Id); 
      favoriteButton.setData(sat.Id); 
      orbitButton.setData(sat.Id); 

      showButton.ValueChanged -= this.showButtonHandler; 
      orbitButton.ValueChanged -= this.orbitButtonHandler; 
      showButton.ValueChanged += this.showButtonHandler; 
      orbitButton.ValueChanged += this.orbitButtonHandler; 
      return cell; 
     } 

도 필요 밤은 :

그래서, 내 코드에 대한 솔루션에 마지막 부분을 변경하는 것입니다. 셀을 다시 사용하면 이전 대리자가 스위치에서 제거 (- =)되고 새로운 새 참조로 바뀝니다 (+ =).

감사합니다.