Have you ever considered using NSNotifications? There are scenarios where using 
notification-type eventing is better than using C# events and delegates 
handlers. This is probably one of those times.

Advantages:
Using notifications don't up (ref++) the object reference counts like C# events 
do. This means GC can clean up your TableView, Source and Cells with very 
little clean up code to unhook events
You don't have to manage how many times events are hooked up. It looks like 
this is the kind of problem you're having.

Just have your TableCell publish a notification and have your TableSource 
observe/subscribe to that notification.


http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html
 


On Monday, September 12, 2011 at 6:40, Dino [via MonoTouch] wrote:

>  Something else to keep in mind that has bitten me in the past: 
> 
> If you present a confirmation dialog, remember that while it's displayed if 
> there's any chance the table could refresh (and thus re-load that cell with a 
> different set of parameters) the wrong "cell" will get acted on instead. This 
> was happening to me because the device was getting TCP communications 
> generated from elsewhere that would alter its list of cells - sometimes 
> removing one, which might cause the cell to be re-populated with different 
> data. 
> 
> Because only one cell could ever have an active confirmation dialog at any 
> given time, I loaded the parameters of that cell into static properties, and 
> acted on the static properties once the user confirmed. 
> 
> Dino 
> 
> -----Original Message----- 
> From: Steve Keane [mailto:[hidden email] 
> (/user/SendEmail.jtp?type=node&node=3807417&i=0)] 
> Sent: Monday, September 12, 2011 8:35 AM 
> To: Dean Cleaver 
> Subject: RE: [MonoTouch] How to add event handler for custom UITableCell 
> button (without it being called many times)? - Email found in subject 
> 
> Thanks. That makes good sense, I will look into that. 
> 
> Thanks for the help! 
> 
> 
> Steve 
> 
> -----Original Message----- 
> From: Dean Cleaver [mailto:[hidden email] 
> (/user/SendEmail.jtp?type=node&node=3807417&i=1)] 
> Sent: 12 September 2011 13:56 
> To: Dean Cleaver; Steve Keane; [hidden email] 
> (/user/SendEmail.jtp?type=node&node=3807417&i=2) 
> Subject: RE: [MonoTouch] How to add event handler for custom UITableCell 
> button (without it being called many times)? - Email found in subject 
> 
> Sorry - not the constructor of the cell as that's only called once. I have 
> properties on the cell that are set during GetCell and the values for that 
> might have been passed in to the constructor of the DataSource. 
> 
> Dino 
> 
> -----Original Message----- 
> From: [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=3) 
> [mailto:[hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=4)] On 
> Behalf Of Dean Cleaver 
> Sent: Monday, September 12, 2011 7:50 AM 
> To: stevek; [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=5) 
> Subject: Re: [MonoTouch] How to add event handler for custom UITableCell 
> button (without it being called many times)? 
> 
> Personally, I pass in the parameters/object it needs to act on to the 
> constructor of the custom cell, and have the event code in the cell perform 
> the required action. 
> 
> Dino 
> 
> -----Original Message----- 
> From: [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=6) 
> [mailto:[hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=7)] On 
> Behalf Of stevek 
> Sent: Monday, September 12, 2011 7:04 AM 
> To: [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=8) 
> Subject: [MonoTouch] How to add event handler for custom UITableCell button 
> (without it being called many times)? 
> 
> I am having trouble trying to and an event handler for a delete button that 
> is on a custom UITableCell 
> 
> Currently the delegate seems to get called several times for each click on 
> the delete button. I am using an anonymous method as I need the indexPath to 
> work out which message to delete. 
> 
> Can anyone suggest a way of ensuring the event is only called once or an 
> alternative method so I can use the indexPath to determine which message to 
> delete without using an delegate in the GetCell method? 
> 
> The code I currently have is: 
> 
> >>>>>>>>>>>>>>>> 
>  public override UITableViewCell GetCell (UITableView tableView, NSIndexPath 
> indexPath) 
>  { 
> SprayCell cell = tableView.DequeueReusableCell (kCellIdentifier) as 
> SprayCell; 
>  if (cell == null) { 
>  cell = new SprayCell(); 
>  var views = NSBundle.MainBundle.LoadNib("SprayCell",cell,null); 
>  cell = Runtime.GetNSObject(views.ValueAt(0)) as SprayCell; 
> 
> //If I do this here it only ever tries to delete the 1st few messages 
>  /* 
>  cell.DeleteButton.TouchUpInside += delegate { 
>  Util.Debug("cell.DeleteButton.TouchUpInside() - DELETE MsgID" + 
> _list2[indexPath.Row].MessageID); 
>  }; 
>  */ 
> 
>  }; 
> 
>  } 
>  cell.Text = _list2[indexPath.Row].Body; 
>  cell.Date1 = "received " + 
> _list2[indexPath.Row].TimeStamp.ToShortTimeString() + " " + 
> _list2[indexPath.Row].TimeStamp.ToShortDateString() ; 
> 
>  cell.DeleteButton.TouchUpInside -= delegate {}; //If I don't have this we 
> get lots of different events firing when we have lots of items in the list 
> 
>  cell.DeleteButton.TouchUpInside += delegate 
> {cell.DeleteButton.TouchUpInside() - DELETE MsgID" + 
> _list2[indexPath.Row].MessageID)}; //Gets called multiple times randomly and 
> regularly at the top/bottom of the list 
> 
>  return cell; 
>  } 
> 
> <<<<<<<<<<<<<<<<< 
> 
> Many Thanks 
> SK 
> 
> -- 
> View this message in context: 
> http://monotouch.2284126.n4.nabble.com/How-to-add-event-handler-for-custom-UITableCell-button-without-it-being-called-many-times-tp3807201p3807201.html
> Sent from the MonoTouch mailing list archive at Nabble.com 
> (http://Nabble.com). 
> _______________________________________________ 
> MonoTouch mailing list 
> [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=9) 
> http://lists.ximian.com/mailman/listinfo/monotouch
> _______________________________________________ 
> MonoTouch mailing list 
> [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=10) 
> http://lists.ximian.com/mailman/listinfo/monotouch
> 
> No virus found in this incoming message. 
> Checked by AVG - www.avg.com (http://www.avg.com) 
> Version: 9.0.901 / Virus Database: 271.1.1/3891 - Release Date: 09/11/11 
> 19:38:00 
> _______________________________________________ 
> MonoTouch mailing list 
> [hidden email] (/user/SendEmail.jtp?type=node&node=3807417&i=11) 
> http://lists.ximian.com/mailman/listinfo/monotouch
> 
> 
> If you reply to this email, your message will be added to the discussion 
> below: 
> http://monotouch.2284126.n4.nabble.com/How-to-add-event-handler-for-custom-UITableCell-button-without-it-being-called-many-times-tp3807201p3807417.html
>  
>  To start a new topic under MonoTouch, email 
> ml-node+s2284126n3346893...@n4.nabble.com 
> (mailto:ml-node+s2284126n3346893...@n4.nabble.com) 
>  To unsubscribe from MonoTouch, click here 
> (http://monotouch.2284126.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=3346893&code=YW5kcmV3ZHlvdW5nQGdtYWlsLmNvbXwzMzQ2ODkzfDE5MjczODc4ODI=).
>  



--
View this message in context: 
http://monotouch.2284126.n4.nabble.com/How-to-add-event-handler-for-custom-UITableCell-button-without-it-being-called-many-times-tp3807201p3808424.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
MonoTouch@lists.ximian.com
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to