6

I'm porting a native objective-C app to a Monotouch solution.

I have a in my storyboard a tableview, with some static table cells.

Using following objective-c code a received a selection from the table

- (void) tableView:(UITableView *) aTableView didSelectRowAtIndexPath:(NSIndexPath *) indexPath
{        // remove the row highlight
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];

    switch(indexPath.row) {
        case 0: [self callWebsite]; break;
        case 1: [self sendEmail]; break;
        case 2: [self makePhoneCall]; break;
    }

    return;
}

Now I want to do the same in MonoTouch, but can't find out how and all the examples I find on the net, are using the DataSource and UITableViewDelegate.

But when using that approach, My "Static" cells are removed, replaced.

This is what I'm trying

public partial class ContactViewController : UITableViewController
{
    public ContactViewController (IntPtr handle) : base (handle)
    {
        this.TableView.Delegate = new CustomTableViewDelegate();
    }
}

public class CustomTableViewDelegate : UITableViewDelegate
{
    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        // Remove the row highlight
        RowDeselected(tableView, indexPath);

        /*
             // Is this section our "button" section?
            switch(indexPath.row) {
                case 0: [self callWebsite]; break;
                case 1: [self sendEmail]; break;
                case 2: [self makePhoneCall]; break;
            }
        */

        Console.WriteLine("Do something meaningfull.");
    }
}

Somebody any suggestion?

  • I do not quite follow, how are your cells removed? The code that you pasted is for the action when the cell is selected, and you have some commented code, why did you comment it out? – miguel.de.icaza Apr 23 '12 at 21:10
  • The commented out code, is a copy paste of my objective-c code, as I was translating it into C#. Is not of any importance for this issue. I'm not sure why the cells are removed. I think it is because I have no implementation of the method 'numberOfRowsInSection' and 'cellForRowAtIndexPath' which are used when creating tables from code. Not statically ... "I THINK ..." – user1346056 Apr 24 '12 at 12:24

1 Answers1

7

I think you should try using WeakDelegate to make this work.

In your controller wire up the method like so:

[Export("tableView:didSelectRowAtIndexPath:")]
public void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
     //Your code here
}

Then set TableView.WeakDelegate = this;

Play around with it, I may not have my parameters exactly right, etc.

jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
  • I tested this out and found that setting the WeakDelegate caused the static cells to be cleared. However if you add the RowSelected method, with the Export attribute, to the owning UITableViewController then you get the expected result. – holmes Apr 24 '12 at 02:24
  • So the static cells you are setting up must set the controller as the delegate for the `UITableView`. What you did is probably the only way to make it work with MonoTouch. This is one thing I want to try out myself, I have not ever used storyboards since they require iOS 5. We are getting to the point where requiring iOS 5 might not be a big deal anymore. – jonathanpeppers Apr 24 '12 at 12:03
  • Indeed implementing this, without the weak delegate assignment works. Thx guys! I would give a vote, but I don't have enough reputation, whoehoeee =P – user1346056 Apr 24 '12 at 12:14