How do I remove/unregister event handler from an event?
I’ve made a custom uitableviewcell . The cell consist out of labels and a UIButton. Everything was working fine until I found out that there is a small problem with the reusable cell in combination with开发者_高级运维 adding an event to an uibutton. So the first time the cell works great when I click the uibutton. But the moment I scroll it starts giving me the wrong detail info. If I comment out the reusable cell part (and let it make a new cell everytime) it just works fine with no problems. So my assumptions is that the event ‘hangs on’ into cell with the old info. I add the event like this:
myButton.TouchDown += delegate{
//some code here
}
Obviously I cannot unregister it like this right? What is the easiest way to overcome this particular problem in monotouch?
You can have anonymous methods and lambdas as event handlers. Just save them as local variables.
EventHandler theHandler = delegate(obj sender, EventArgs e)
{ Console.WriteLine("The handler!"); };
//... or: EventHandler theHandler = (sender, args) => { Console.WriteLine("The handler!"); };
private void HookEvent()
{
myButton.TouchDown += this.theHandler;
}
private void UnhookEvent()
{
myButton.TouchDown -= this.theHandler;
}
If you want to be able to unregister your event handler, you shouldn't use anonymous methods (or lambdas). Move your code into an actual method and register it like this:
private void MyMethod()
{
//some code here
}
// register
myButton.TouchDown += MyMethod;
// unregister
myButton.TouchDown -= MyMethod;
OK. I've been trying a lot of stuff to solve this but ChrisNTR came with the right solution to my particular problem. I will show his example snippet here:
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell("reuseThis");
if(cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, "reuseThis");
}
cell.TextLabel.Text = Convert.ToChar(dash.oneToHundred[indexPath.Row]).ToString();
var button = UIButton.FromType(UIButtonType.RoundedRect);
button.Frame = new System.Drawing.RectangleF(40f, 0f, 280f, 40f);
button.SetTitle(dash.oneToHundred[indexPath.Row].ToString(),UIControlState.Normal);
button.AddTarget (this, new Selector ("MySelector"), UIControlEvent.TouchDown);
button.Tag = indexPath.Row;
cell.ContentView.AddSubview(button);
return cell;
}
[Export ("MySelector")]
void ButtonEventHere (UIButton button)
{
Console.WriteLine ("Hello! - Button clicked = " + button.Tag );
Console.WriteLine ( Convert.ToChar(dash.oneToHundred[button.Tag]).ToString() );
}
In short you call button.AddTarget() and you pass the corresponding values to the button. one of these values is the selector which you defined as 'buttonEventHere' (can be any name you want it to me my sure that the [export("MySelector")] has the right name). Make sure you pass the indexPath.Row to the button.Tag. You will use that index to know which button was pressed and do your action accordingly.
Hope this helps if you was stuck as i was.
You might want to use this technique to work out which UIButton was pressed rather than trying to use custom event args which is going to be problematic - http://jainmarket.blogspot.com/2009/05/custom-button-in-uitableviewcell.html
Essentially you just work out what section/row was selected in the UITableViewCell. From there you should be able to work out what UIButton was selected.
Hope this helps,
ChrisNTR
精彩评论