Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
785
Button Column Definition
posted

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.

Parents
  • 40030
    Offline posted

    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

Reply Children
No Data