Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch How to implement RowSelected for a UITableViewController with Static Cells

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?

like image 366
user1346056 Avatar asked Apr 23 '12 07:04

user1346056


1 Answers

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.

like image 129
jonathanpeppers Avatar answered Oct 28 '22 00:10

jonathanpeppers