Hey - I'm hoping you guys can help me out again. I'm looking to create a custom Column Definition that has a UIButton inside it. I can pull it off, but when I add an event handler to the button that is being created I get an app crash. I've done some research on it and it has to do with the bridgeConnector or something along those lines. I was wondering if you guys had an example of how to pull this off? Each row in the grid should have a button column, reference the row that was clicked and well just display an alert with the row index or something on TouchUpInside.
Let me know if this does not make any sense or if you need more information.
Hi,
Sure thats no problem.
Basically you just need to create a custom column and custom cell. In the code below they are ButtonColumn and ButtonCell. With the button, i use the AddTarget option and use a selector. You need to make sure you mark the method you want to invoke with an Export that matches the name of the selector though. I've highlighted the code below:
public class ButtonCell : IGGridViewCell { UIButton _button; public ButtonCell(string id) :base(id) { _button = new UIButton (UIButtonType.RoundedRect); _button.AddTarget(this, new MonoTouch.ObjCRuntime.Selector("Clicked"), UIControlEvent.TouchUpInside); this.AddSubview (_button); } [Export("Clicked")] public void Clicked() { _button.SetTitle ("Pressed", UIControlState.Normal); } public void SetText(string text) { _button.SetTitle (text, UIControlState.Normal); } public override void SetupSize (SizeF size) { _button.Frame = new RectangleF (0, 0, size.Width, size.Height); } } public class ButtonColumn : IGGridViewColumnDefinition { public ButtonColumn() { } public override string FieldKey { get { return "BC"; } } public override IGGridViewCell CreateCell (IGGridView gridView, IGCellPath path, IGGridViewDataSourceHelper dataSource) { ButtonCell cell = (ButtonCell)gridView.DequeueReusableCell ("BC"); if (cell == null) { cell = new ButtonCell ("BC"); } cell.SetText ("PUSH"); return cell; } }
Hope this helps,
-SteveZ
Hi Stephen,
do you have a full functional example in c#.
I have button inside of a cell (very similar code as in your example) but somehow OnClick is not being triggered. Also, say I have normal and highlited images assigned to the button, should highlited mode functionality still work for a button inside of a cell?
Thank you
Mark
Hi Mark,
Sure, i've attached a sample that shows how to create a button column in C#.