Forum Xamarin.iOS


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to add a custom button in a table cell

I am creating a mobile Application in xamarin ios.I want to add a button in a table cell(in every cell).so i have already go through following Link's_appearance/. And also on that button's click i need the data of that particular cell.
I don't need the cell click but i want the same thing should happen on my custom button's click .I can
handle my cell click by following code-
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
new UIAlertView("Row Selected", tableItems[indexPath.Row], null, "OK", null).Show();
tableView.DeselectRow (indexPath, true); // normal iOS behaviour is to remove the blue highlight

but I need this should happen on my cell's button click.Please help me.


  • WilliamThomasWilliamThomas USMember ✭✭
    edited April 2014

    You should first distinguish between "selecting" the table's row from what you're looking to create. If you allow selections on your table then the entire row will be clickable and will invoke the handler in your table's source.

    Instead, you should do the following things:

    1. Set the AllowsSelection property of your UITableView to false.
    2. Create a UIButton inside your custom UITableViewCell, as well as a public event with an event handler which will provide the data of the cell you require.
    3. Add a handler for your new button's TouchedUpInside event, which in turn will invoke the new event you created in your cell.
    4. In your custom UITableViewSource, add another public event (most likely with the same type of handler as the first one).
    5. In your source's overridden GetCell method, subscribe to the cell's event you created with an event handler that will invoke the event defined in the source.
    6. Finally, in your view controller, subscribe to the source's event in order to catch the cell's button click.

    Essentially the event containing the data of the cell will "bubble up" to your view controller where you need to handle it.

  • RichardCookRichardCook USMember ✭✭
    edited September 2014

    I think an easier way to do this is to add a property to your custom cell class

    int currentIndex;

    Then in the in your table data source, when GetCell is called and then it calls the UpdateCell method on your custom cell class, set this property to the index of your row when the cell uses it.

    In your button handler, you will have access to the currentIndex property.

    public class CustomProjectCell : UITableViewCell  {
                UIButton btnProjectName;
                int currentIndex;
                public CustomProjectCell (NSString cellId) : base (UITableViewCellStyle.Default, cellId)
                    btnProjectName = new UIButton(UIButtonType.System);
                    btnProjectName.TouchUpInside += (sender, e) => {
                        new UIAlertView("Project Touched: ", this.currentIndex.ToString(), null, "OK", null).Show();
    public void UpdateCell (int index, string projectName)
                    currentIndex = index;
                    btnProjectName.SetTitle(projectName, UIControlState.Normal);
  • RichardCookRichardCook USMember ✭✭

    Even better, I just found that UITableViewCell has a property called Tag that is typically used for this scenario. I just saw it used in one of the sample projects.

    So your custom cell class already has access to this. No need to have the currentIndex property defined in your custom cell class.


Sign In or Register to comment.